I'm making a game where you have to collect donuts, and there is an area that will only allow players with 200 donuts or more to enter. I have used a stat door script and a teleporter script to make it teleport the player to the place. It works fine, but breaks after you teleport several times. When the part is touched, it teleports the player to another part in the game.
This is the script:
function getPlayer(humanoid)
local players = game.Players:children()
for i = 1, #players do
if players[i].Character.Humanoid == humanoid then return players[i] end
end
end
Door = script.Parent
function onTouched(hit)
print("Door Hit")
local human = hit.Parent:findFirstChild("Humanoid")
if (human ~= nil ) then
-- a human has touched this door!
print("Human touched door")
-- test the human's name against the leaderboard
local player = getPlayer(human)
if (player == nil) then return end
local stats = player:findFirstChild("leaderstats")
local sp = stats:findFirstChild("Donuts") --Change level to stat name.
if sp == nil then return false end
if (sp.Value >= 200) then --Change 1 to the number needed to pass
local Teleport = "GrassMapEnterence" --Put the name of the Part between the ""s.
function Touch(hit) --Indicates that the Part has been Touched.
if script.Parent.Locked == false and script.Parent.Parent:findFirstChild(Teleport).Locked == false then script.Parent.Locked = true script.Parent.Parent:findFirstChild(Teleport).Locked = true --Checks Debounce.
local Pos = script.Parent.Parent:findFirstChild(Teleport) --Gets the Part to teleport to.
hit.Parent:moveTo(Pos.Position) wait(1) script.Parent.Locked = false script.Parent.Parent:findFirstChild(Teleport).Locked = false end end --Takes you there and Ends the Function.
script.Parent.Touched:connect(Touch) --Listens out for Touchers.
end
end
end
connection = Door.Touched:connect(onTouched)
Can anyone help me fix it? It works but stops working after you teleport several times.

2 comments
*breaks
sorry i was rushing it