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

[SOLVED] My scripts breaks after it's run a few times, can anyone help?

Asked by 5 years ago
Edited 5 years ago

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.

0
*breaks EliteRayGawnX2 124 — 5y
0
sorry i was rushing it OrbitOps 34 — 5y

1 answer

Log in to vote
0
Answered by
yHasteeD 1819 Moderation Voter
5 years ago

Do not use hit function with another hit function and you no need to use a function to get player, i created new script for you:

wait(0.3)
local Door = script.Parent -- Door local
local TeleportPartLocation = script.Parent.Parent.Part2 -- Teleport Brick
script.Parent.Locked = true; -- Anti btools!
local donutsneed = 200 -- Value to enter

function onTouched(h) -- On touch
    print("Door hit") -- print "Door hit"
    if h.Parent:FindFirstChild("Humanoid") then -- Find Humanoid
        local plr = game:GetService("Players"):FindFirstChild(h.Parent.Name) -- Find player
        local stats = plr:FindFirstChild("leaderstats") -- Get leadersats
        local donut = stats:FindFirstChild("Donuts") -- Find donuts
        if stats then -- If found stats
            if donut then -- If found donut
                if donut.Value >= donutsneed then -- Detect if you have donuts for use teleport
                    h.Parent:MoveTo(TeleportPartLocation.Position) -- Teleport you to block
                    return true;
                end
            else
                return false;
            end
        else
            return false;
        end
        return false;
    elseif h.Parent.Parent:FindFirstChild("Humanoid") then
        local plr = game:GetService("Players"):FindFirstChild(h.Parent.Parent.Name) -- Find player
        local stats = plr:FindFirstChild("leaderstats") -- Get leadersats
        local donut = stats:FindFirstChild("Donuts") -- Find donuts
        if stats then -- If found stats
            if donut then -- If found donut
                if donut.Value >= donutsneed then -- Detect if you have donuts for use teleport
                    h.Parent:MoveTo(TeleportPartLocation.Position) -- Teleport you to block
                    return true;
                end
            else
                return false;
            end
        else
            return false;
        end
        return false;
    else
        print("Cannot teleport :(") -- print "Cannot teleport :("
        return false;
    end
end

Door.Touched:Connect(onTouched)

If you want a delay use debounce.

0
@yHasteeD Thank you, this really helps! OrbitOps 34 — 5y
0
Hastee i love your willingness to help! Your formatting is always very nice to look at too. I do have to ask though why do you always make functions with a .Parent and also a .Parent.Parent? Is there a purpose? DinozCreates 1070 — 5y
0
If not find hit.Parent should try to hit.Parent.Parent, for me it avoids many mistakes. yHasteeD 1819 — 5y
Ad

Answer this question