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.

1script.Parent.Touched:connect(function(player)
2    local tool = player.Backpack.Ticket
3    if tool ~= nil then
4        script.Parent.CanCollide = false
5        wait(.5)
6        script.Parent.CanCollide = true
7        else
8    end
9    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

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

You needed to define the player by using GetPlayerFromCharacter:

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

Please accept my answer if this helped!

Answer this question