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

How do I make a script that only works once on a same player?

Asked by 5 years ago
Edited by DanzLua 5 years ago

I want to make a trip brick that makes player fall but I only want it to work once for every player. More specific,I am making a zombie virus by heavily modifying a zarm. Now I want to make it so that when someone gets infected he falls to the ground for a few seconds but only once in his lifetime because when I test it zombies trip other zombies and it looks stupid so I want to make it only work once on every player and when the player resets or dies,he can then be tripped again. On my attempt to make it work I tried sticking some zarm lines into the code but failed,I know that "if cantouch = 0/1 then" lines do what I want but I can't seem to figure out how to do the same with this code,sorry but first few lines are in this part of the text and I did not know how to fix that:

b = script.Parent
b.Touched:connect(function(hit)
    local c = hit.Parent
    if c then
        for _,h in next, c:GetChildren() do
            if h:IsA'Humanoid' and h.Health > 0 and h:GetState().Name ~= 'Physics' then


                local newscript = script.physics:Clone()
                newscript.Parent = c
                newscript.Disabled = false

                wait(10)

                if h.Parent then
                    local newscript = script.gettingup:Clone()
                    newscript.Parent = c
                    newscript.Disabled = false
                end
                break
            end
        end
    end
end)

0
Use the code block properly Prestory 1395 — 5y

1 answer

Log in to vote
0
Answered by
DanzLua 2879 Moderation Voter Community Moderator
5 years ago
Edited 5 years ago

You have to keep track of who touches it and refer back to it on a new touch

--leave this false if you want it to that the current player cant touch it more than once
--change to true if the overall player can only touch it once
local OnePlayerTouchPerSession=false

local touch={}

b = script.Parent
b.Touched:connect(function(hit)
    local c = hit.Parent
    if c and c:FindFirstChild("Humanoid") and game.Players:GetPlayerFromCharacter(c) then
    local plr=game.Players:GetPlayerFromCharacter(c) 
        for _,v in pairs(touch) do
              if v==(not OnePlayerTouchPerSession and c or plr) then
                        return
                else
                        table.insert(touch,(not OnePlayerTouchPerSession and c or plr))
                end  
        end
        for _,h in next, c:GetChildren() do
            if h:IsA'Humanoid' and h.Health > 0 and h:GetState().Name ~= 'Physics' then



                local newscript = script.physics:Clone()
                newscript.Parent = c
                newscript.Disabled = false

                wait(10)

                if h.Parent then
                    local newscript = script.gettingup:Clone()
                    newscript.Parent = c
                    newscript.Disabled = false
                end
                break
            end
        end
    end
end)
Ad

Answer this question