Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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)

1 comment

0

wth, suppose a part touches this parts script then it will search for the backpack of the part... Wth

2 answers

✓ Accepted answer

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)

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!

This discussion is preserved as it was. New answers, comments, and votes are closed.