这是我在模型本身中生成的代码,该代码位于 ReplicatedStorage 中,这是层次结构: SpawnDoor(模型) 门(声音) 光(文件夹) 门1(部分) 门2(部分) 部分(部分) 表面光(SurfaceLight) Teleport(脚本) - 当门生成时脚本不起作用 木材(文件夹) 门1(型号) 联盟 门2(型号) 联盟
local TweenService = game:GetService("TweenService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local CameraShaker = require(ReplicatedStorage:WaitForChild("CameraShaker"))
local player = game.Players.LocalPlayer
local playerGui = player.PlayerGui.Red.Red
local Camera = game.Workspace.CurrentCamera
local cooldown = false -- Cooldown flag
local cooldownTime = 2 -- Cooldown time in seconds
local function ShakeCamera(shakCf)
Camera.CFrame = Camera.CFrame * shakCf
end
local renderPriority = Enum.RenderPriority.Camera.Value + 1
local camShake = CameraShaker.new(renderPriority, ShakeCamera)
camShake:Start()
-- Function to handle J key press
local function onKeyPress(input)
if input.KeyCode == Enum.KeyCode.J then
if cooldown then
print("Tool is on cooldown")
-- Shake camera and show GUI
camShake:ShakeOnce(60, 80, 1, 1) -- Adjust parameters as needed
local transparencyTweenInfo = TweenInfo.new(1, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, 0, false, 0)
local tween1 = TweenService:Create(playerGui, transparencyTweenInfo, {
BackgroundTransparency = 0.3
})
local tween2 = TweenService:Create(playerGui, transparencyTweenInfo, {
BackgroundTransparency = 1
})
tween1:Play()
tween1.Completed:Connect(function()
tween2:Play()
end)
return
end
cooldown = true -- Activate cooldown
print("Tool Activated") -- Check if this prints when you use the tool
game.SoundService.Biwa:Play()
local Player = game.Players.LocalPlayer
local Char = Player.Character
if not Char then
print("Character not found")
cooldown = false -- Reset cooldown if an error occurs
return
end
local HRP = Char:FindFirstChild("HumanoidRootPart")
if not HRP then
print("HumanoidRootPart not found")
cooldown = false -- Reset cooldown if an error occurs
return
end
local DoorModel = game.ReplicatedStorage.SpawnDoor
if not DoorModel or not DoorModel:IsA("Model") then
print("SpawnDoor Model not found in ReplicatedStorage or not a Model")
cooldown = false -- Reset cooldown if an error occurs
return
end
-- Raycast to find the ground below the player
local rayOrigin = HRP.Position
local rayDirection = Vector3.new(0, -100, 0) -- Cast downwards
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {Char}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
local raycastResult = workspace:Raycast(rayOrigin, rayDirection, raycastParams)
if not raycastResult then
print("No ground detected below the player")
cooldown = false -- Reset cooldown if an error occurs
return
end
local groundPosition = raycastResult.Position
local door = DoorModel:Clone()
door.Parent = workspace
-- Set the PrimaryPart of the door model
door.PrimaryPart = door:FindFirstChild("Light").Part
if not door.PrimaryPart then
print("PrimaryPart not found in DoorModel")
door:Destroy()
cooldown = false -- Reset cooldown if an error occurs
return
end
-- Set the position of the door model to the ground position
door:SetPrimaryPartCFrame(CFrame.new(groundPosition))
print("Door spawned at:", door.PrimaryPart.Position)
-- Delay before sliding doors open
wait(2)
-- Animation to slide open doors
local door1 = door.Wood.Door1
local door2 = door.Wood.Door2
if door1 and door2 then
if door1.Union and door2.Union then
-- Tween for Door1
local door1OpenCFrame = door1.Union.CFrame * CFrame.new(0, 0, -5) -- Example: Move 5 studs to the right
local door1Tween = TweenService:Create(door1.Union, TweenInfo.new(0.2), {CFrame = door1OpenCFrame})
-- Tween for Door2
local door2OpenCFrame = door2.Union.CFrame * CFrame.new(0, 0, 5) -- Example: Move 5 studs to the left
local door2Tween = TweenService:Create(door2.Union, TweenInfo.new(0.2), {CFrame = door2OpenCFrame})
door1Tween:Play()
door2Tween:Play()
game.SoundService.Door:Play()
else
print("Primary part not found in one of the doors")
end
else
print("One of the doors not found in DoorModel")
end
print("Doors sliding open")
task.wait(1)
-- Destroy the door model after a delay
repeat
door.Wood.Door1.Union.Transparency += 0.01
door.Wood.Door2.Union.Transparency += 0.01
door.Light.Part.Transparency += 0.01
door.Light.Door1.Transparency += 0.01
door.Light.Door2.Transparency += 0.01
task.wait(0.01)
until door.Wood.Door1.Union.Transparency >= 1
door:Destroy()
print("Door destroyed after 3 seconds")
-- Start cooldown timer
task.delay(cooldownTime, function()
cooldown = false
print("Cooldown ended")
end)
end
end
-- Connect the function to the key press event
game:GetService("UserInputService").InputBegan:Connect(onKeyPress)
我尝试从工作区克隆它,并尝试拥有一个不断更新的服务器脚本来检查要生成的门,然后执行我想要的操作,我什至用一个简单的打印脚本替换了该脚本所有情况都是为了看看我的代码是否错误,但似乎没有任何效果。
如果这个脚本是本地的(我认为是)。当您克隆本地脚本时,它会由于某种原因停止工作。尝试将您发布的克隆和脚本放在服务器端。我前段时间也遇到过同样的问题。希望这有帮助:)