This is my code for spawning in the model itself which is located in ReplicatedStorage, which this is the hierarchy:
SpawnDoor (Model)
Door (Sound)
Light (Folder)
Door1 (Part)
Door2 (Part)
Part (Part)
SurfaceLight (SurfaceLight)
Teleport (Script) –script that doesn’t work when door spawns in
Wood (Folder)
Door1 (Model)
Union
Door2 (Model)
Union
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)
I’ve tried to clone it from the workspace instead as well as trying to have a constantly updating server script that checks for the door to spawn in and then does the action I wanted, I even replaced the script with a simple print script for all cases to see if maybe my code was wrong but nothing seems to work.
Sergei Ivlev is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
If this script is local (which I think it is). When you clone a local script it stops working, for some reason. Try having the clone and script you posted server-sided. I had this same issue some time ago. Hope this helps 🙂