Door Script not working

Can anyone help me fix this scirpt

local ProximityPrompt = script.Parent:WaitForChild("ProximityPrompt") -- Assuming the ProximityPrompt is a child of the door
local opened = false
local Sound = script.Parent:WaitForChild("Sound") -- Assuming the sound is a child of the door

-- Ensure the PrimaryPart is set
script.Parent.PrimaryPart = script.Parent:WaitForChild("DoorPart") -- Replace "DoorPart" with the actual part you want to rotate (e.g., the door's main part)

-- Function to open and close the door
local function ToggleDoor()
	if opened == false then
		opened = true
		Sound:Play()
		for i = 1, 21 do
			-- Rotate the door around its PrimaryPart (assuming the part you've set as PrimaryPart is the hinge point)
			script.Parent:SetPrimaryPartCFrame(script.Parent.PrimaryPart.CFrame * CFrame.Angles(0, math.rad(4.6), 0))
			wait()
		end
	else
		opened = false
		Sound:Play()
		for i = 1, 21 do
			-- Rotate the door back to its original position
			script.Parent:SetPrimaryPartCFrame(script.Parent.PrimaryPart.CFrame * CFrame.Angles(0, math.rad(-4.6), 0))
			wait()
		end
	end
end

-- Connect the ProximityPrompt's action to toggle the door
ProximityPrompt.Triggered:Connect(function(player)
	ToggleDoor()
end)

Is your model having a PrimaryPart set?

based on the comments it looks like you’ve had an ai write it? dont expect everything that ai makes is going to work
Anyways i dont have a lot of time right now but ill look at the script once i do

1 Like

yes i used ai to fix it but they didnt fix it

i dont know what this is lol how do i add it

Click on your model and go to properties menu and find property called PrimaryPart click on that and then you go to the main part of the door and click on it and you should see that property became smth like: Primary Part : (PartName)

oh wait it worked but how to make it rotate like a door and not stay in play and rotate heres on the video how it works

You have to do some Hinge calculations, like get the Position of the PrimaryPart, Add a Vector3 to the X Axis. This vector will be the negative of the position of the PrimaryPart divided by 2, Add a Rotation with CFrame Angles on the Y Axis, this will be used to rotate the door around the hinge.

local hingeOffset = Vector3.new(-script.Parent.PrimaryPart.Size.X / 2, 0, 0) 
local hingePoint = script.Parent.PrimaryPart.Position

-- inside the function:
local rotation = CFrame.Angles(0, math.rad(4.6), 0) 
local offset = script.Parent.PrimaryPart.CFrame:pointToObjectSpace(hingePoint) 
script.Parent:SetPrimaryPartCFrame(CFrame.new(hingePoint) * rotation * * CFrame.new(-offset))

Try getpiviot and pivotto instead

And change your part’s primarypart , to a smaller part at the corner that rotates the angle of the whole model , you get what i mean?

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.