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

How do you disable a script just for the player that activated it?

Asked by 2 years ago
Edited 2 years ago

So I added some money to my game but when a player touches it, it repeats the script and gives more money than it should. However, I created a code that makes the script only work once but when another player touches the money they don't get any.

I'm wondering if there's a way to disable the script locally and not get any extra money but for others to be able to get the money too.

Heres my code:

    WaitTime = 999999
    Amount = 25
    function onTouched(part)
        local h = part.Parent:findFirstChild("Humanoid")
        if (h~=nil) then
            local thisplr = game.Players:FindFirstChild(h.Parent.Name)
            if (thisplr~=nil) then
                local stats = thisplr:findFirstChild("leaderstats")
                if (stats~=nil) then
                    local Score = stats:findFirstChild("cash")
                    if (Score~=nil) then
                        Score.Value = Score.Value + Amount
                        end
                    end
                end
            end
        end
    script.Parent.Touched:Connect(onTouched)

1 answer

Log in to vote
0
Answered by 2 years ago

Yeap there is a way in my case I used a table to insert their names when they get it and used a find function to see if they were in that table if not then it would give them money, otherwise it would print "ALREADYGOTIT".

WaitTime = 999999
Amount = 25

local alreadygotit = {}

function onTouched(part)
    local h = part.Parent:findFirstChild("Humanoid")
    if (h~=nil) then
        local thisplr = game.Players:FindFirstChild(h.Parent.Name)
        if (thisplr~=nil) then
            local CHECKER = table.find(alreadygotit, thisplr.Name)

            if CHECKER == nil then
            local stats = thisplr:findFirstChild("leaderstats")
            if (stats~=nil) then
                local Score = stats:findFirstChild("cash")
                if (Score~=nil) then
                    table.insert(alreadygotit, thisplr.Name)
                    Score.Value = Score.Value + Amount
                end
            end
            else
                print("ALREADY GOT IT")
            end
        end
    end
end
script.Parent.Touched:Connect(onTouched)
0
Thank you so much! I wasted so much time trying to fix this and you helped me understand. delirik_1446 4 — 2y
Ad

Answer this question