Door Script Is Not Working

My door script isn’t working, I want it to open once a player reaches with in 5 studs of the door, he is my script:


local RP = script.Parent.PrimaryPart
local Radius = 5
local DetectionPart = script.Parent.Detectionpart
local TweenService = game:GetService("TweenService")
local DoorTween = TweenService:Create(RP, TweenInfo.new(0.5,Enum.EasingStyle.Bounce), {CFrame = RP.CFrame * CFrame.Angles(0,math.rad(90),0)})
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(plr)
	local Character = plr.Character or plr.CharacterAdded:Wait()

	local HRP = Character.HumanoidRootPart
	if (HRP.Position - DetectionPart.Position).Magnitude <= Radius then
		DoorTween:Play()
	end
	if DoorTween.Completed then
		print("yes") --for some reason this thing prints first thing when I test it.
	end
end)

So yeah, it isn’t working. I put a note on the final “if” statement that it prints yes when I test the game, so it is playing even though I’m not in the radius, but even weirder, its not even changing the position, it just stays in the same position, (I want it to tween 90 degress but it isn’t, even though its printing yes that it did). I tried to see if anyone has had this problem as well. No one to my knowledge
As you can see, It printed “yes” but it didn’t actually tween 90 degress + its not even suppose to be printing yes!


this is a picture when I get in the 5-stud range:

as you can see, Nothing happens. Its a script under the whole door model btw:
image

1 Like

Tween.Completed is an event, not a boolean. Since the event exists, it’ll return true.

1 Like

Ohhh, How do I check if its done then?

Just connect to it like a normal event:

Tween.Completed:Connect(function()
   -- do stuff
end)
1 Like

Ah ok, so now its not printing anything. Now the problem is that it just doesn’t work at all.

The issue is that it only checks one time. Your code should be:

local RP = script.Parent.PrimaryPart
local Radius = 5
local DetectionPart = script.Parent.Detectionpart
local TweenService = game:GetService("TweenService")
local DoorTween = TweenService:Create(RP, TweenInfo.new(0.5,Enum.EasingStyle.Bounce), {CFrame = RP.CFrame * CFrame.Angles(0,math.rad(90),0)})
local Players = game:GetService("Players")
local isPlaying = false

Players.PlayerAdded:Connect(function(plr)
    game:GetService("RunService").Heartbeat:Connect(function()
	    local Character = plr.Character or plr.CharacterAdded:Wait()

	    local HRP = Character.HumanoidRootPart
	    if (HRP.Position - DetectionPart.Position).Magnitude <= Radius and not isPlaying then
           isPlaying = true
		   DoorTween:Play()
	    end
    end)

    DoorTween.Completed:Connect(function()
        isPlaying = false
		print("yes") 
	end)
end)
1 Like

Woah! Thanks. Now I just got to debounce the printing thing, Thanks so much!