Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Why wont this tool door script work in both scripts and localscripts but works in studio?

Asked by
Zeluxis 100
7 years ago

Trying to make a door that only lets you pass if you have a certain tool. It works in studio, but wont work in game despite the fact I've had it in a script and a localscript. This is the code.

script.Parent.Touched:connect(function(player)
    local tool = player.Backpack.Ticket
    if tool ~= nil then
        script.Parent.CanCollide = false
        wait(.5)
        script.Parent.CanCollide = true
        else
    end
    end)
0
wth, suppose a part touches this parts script then it will search for the backpack of the part... Wth greatneil80 2647 — 7y

2 answers

Log in to vote
0
Answered by 7 years ago

the Touched event does not automatically send the player as a parameter. Instead, it sends the part that touched script.Parent

script.Parent.Touched:connect(function(part)
    local player = game.Players:GetPlayerFromCharacter(part.Parent) --checks if a player's limb touched it, then gets its player.
    if player then
        local tool = player.Backpack.Ticket
        if tool then -- "~= nil" is not necessary
            script.Parent.CanCollide = false
            wait(.5)
            script.Parent.CanCollide = true
        end -- "else" wasn't necessary either.
    end
end)
Ad
Log in to vote
0
Answered by 7 years ago

You needed to define the player by using GetPlayerFromCharacter:

script.Parent.Touched:connect(function(p)
    local player = game.Players:GetPlayerFromCharacter(p.Parent) --You needed to get the player who touched it.
    local tool = player.Backpack:WaitForChild("Ticket")
    if tool ~= nil then
        script.Parent.CanCollide = false
        wait(.5)
        script.Parent.CanCollide = true
        else
    end
    end)

Please accept my answer if this helped!

Answer this question