Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

Chest Script Gives Infinite Money Instead of Set Amount?

Asked by 6 months ago

Hi, this chest script works by being touched, giving the player their gold and sending the chest to Lighting as a way to make it despawn for 45 seconds. The thing is, this script works when picking one up one at a time or small amounts of chests are being used in the map. However, when there are lots of players are on and there are lots of chests, the chests will break and be able to be touched and give money without despawning into Lighting, giving players infinite money basically. When I tried to assign the chests random numbers in their name so the script knows which specific chest to put back into position, this solution did not end up working.

What could be the problem here?

amnt = math.random(50,100)
chest = script.Parent
chest.Name = ("Chest"  .. math.random(0,500))



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("Gold")
                if (score~=nil) then
                    score.Value = score.Value + amnt
                    print("Touch")
                    chest.Parent = game.Lighting
                    wait(45)
                    game.Lighting:FindFirstChild(chest.Name).Parent = game.Workspace
                end
            end
        end
    end
end

script.Parent.Special.Touched:connect(onTouched)

3
Use a debounce. Rheines 600 — 6mo
0
You should use GetPlayerFromCharacter not findFirstChild from players. It is very simple: local thisplr = game.Players:GetPlayerFromCharacter(h.Parent) suspectshot108 386 — 6mo

1 answer

Log in to vote
0
Answered by 6 months ago

The problem is that touching something most often touches it dozens of times at once, since your character is moving while touching it. I think a better way to handle this is by having it give you the amount when you Click on it rather than touch it.

0
True, however after those dozen touches the chest still disappears. For me this is not the case, The players can stand on the chest and infinitely collect from it without it ever dissappearing GlobeHousee 48 — 6mo
0
If the server lags you can still click it more than once until the chest disappears. Rheines 600 — 6mo
Ad

Answer this question