Why does my script not play my sliding door tween?

I am trying to make a door that slides when a person under the “MentalDoctor” team touches the doorTrigger. Without the script to detect the team, the tween plays perfectly fine. After putting my team detection script, nothing happens. I do not receive an error.

script.Parent.doorTrigger.Touched:Connect(function(hit)
	local character = hit.Parent
	local localplayer = game.Players:GetPlayerFromCharacter(character)
	if localplayer.Team == "MentalDoctor" then
	tween1open:Play()
	tween2open:Play()
	unionOpen:Play()
	wait(2)
	tween1close:Play()
	tween2close:Play()
	unionClose:Play()
	elseif localplayer.Team == not "MentalDoctor" then
		return end
end)```

To only play the animation when a person from MentalDoctor touches the door, do this:

local MentalDoctor = game:GetService("Teams")["MentalDoctor"]

local doorOpen = false

local function openDoor()
    tween1open:Play()
    tween2open:Play()
    unionOpen:Play()
    wait(2)
    tween1close:Play()
    tween2close:Play()
    unionClose:Play()
    wait(2)
    doorOpen = false
end

script.Parent.doorTrigger.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        local humanoid = hit.Parent:FindFirstChild("Humanoid")
        local character = humanoid.Parent
        local localplayer = game:GetService("Players"):GetPlayerFromCharacter(character)
        -- Rest of code
        if doorOpen == false and localplayer.Team == MentalDoctor then
            doorOpen = true
            openDoor()
        end
    end
end)

Note that I made an openDoor() function, just in case you would like to repeat this code elsewhere.


Edit

  • Added debounce
  • Fixed checking team

When posting code to the DevForum, please use a code block. You can use three backticks or use the code block tool in your post tools.

```lua
– Your code
```

Anyway, although there’s a few other issues I can spot, it’d be irrelevant to point them out. The main reason why your door stops working is because you have an unnecessary elseif statement which calls return if a player’s team is not MentalDoctor. Get rid of the elseif, you don’t need to account for a non-true condition in this scenario.

I get an error saying that MentalDoctors is nil, I am sure that MentalDoctor exists and is named MentalDoctor.

I was just trying to make sure that there wasn’t an error or anything, that’s why I tried to handle if the player was not MentalDoctor then to return end, the script still doesn’t work after removing that however

It’s a typo. Change ["MentalDoctor"] to ["MentalDoctors"].


If that’s not the case, please send an image of the output.

If you’re using the code Ultimate provided (under final product), get rid of GetTeams. That’s not necessary to have, it does a table index lookup and the index to MentalDoctors is nil.

local MentalDoctors = game:GetService("Teams")["MentalDoctors"]

Also, a tip - don’t use GetPlayers here. The list won’t update later. New players that join the team won’t be able to access the door. Just check if Player.Team matches MentalDoctors.

1 Like
local debounce = false
local MentalDoctors = game:GetService("Teams")["MentalDoctor"]

local function openDoor()
	if debounce == false then
    tween1open:Play()
    tween2open:Play()
    unionOpen:Play()
	debounce = true
    wait(2)
    tween1close:Play()
    tween2close:Play()
    unionClose:Play()
	debounce = false
	end
end

script.Parent.doorTrigger.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        local humanoid = hit.Parent:FindFirstChild("Humanoid")
        local character = humanoid.Parent
        local localplayer = game:GetService("Players"):GetPlayerFromCharacter(character)
        if localplayer.Team == MentalDoctors then
            openDoor()
        end
    end
end)

I have the same outcome as I did originally, no error and no movement in the door. I took the advice from both of you, including checking if localplayer.Team == MentalDoctors.

edit: to be clear, my team name is “MentalDoctor”

Print something in the output in the function to see if the function fires. It could just be an error with the tweening or the rest of your code.

I have a feeling that it’s not your code then because this doesn’t have any errors. What are the assignments of the tween variables? I feel like those are the root causes. If there’s no errors in your door and the code looks fine, it’s something else - I suspect the tweens.

It still seems to be an issue with finding the team. The touched function is fired, it just cannot find whether I am in the team MentalDoctor or not.

I’d recommend you copy and paste my code. Also, if the function fires, then it’s a problem with your tweens.

I think I might have found my issue, my TeamColor was set to white when I had other teams also set to white, thanks for helping!

Lmao that’s funny. So does my edit work now?

Yes, I think my script was working in the first place too but yours helped me learn how to make it more efficient.

2 Likes