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

Give player money every x seconds. Wait function is not working??

Asked by 4 years ago

I dont know why but the wait function doesnt seem to be working

while allPlayersAlive() do
    wait(2.5)
    giveMoneyToPlayers() -- all players in a region give 5$
    if allFinish() then -- returns true if all players are in brick region
        teleportToStart()
        break
    end
end

so basically the loops is suppose to wait 2.5 seconds then

get all players in a room (players found using region 3) and give them 5$

then checks the finish region, and if all players are there, teleport them to the start and break the while loop

what is happening is it waits 2.5 seconds and then adds 80$ to the players balance, instead of 5$

im not sure why this is happening please help.

0
What is the "break" for? User#25069 0 — 4y
0
Also, I might be able to work out what's going on if you provide the GiveMoneyToPlayers() function. User#25069 0 — 4y
0
If it continues to loop and give 80 dollars each time, it seems more like a problem of the cash giving function than the loop. CeramicTile 847 — 4y
0
to exit the loop, i forgot to add after the loop there is a cleanup() function JoshChubi 74 — 4y
0
@Quezzert Just posted the giveMoney() function JoshChubi 74 — 4y

2 answers

Log in to vote
0
Answered by 4 years ago

Change line 11 to "if r == player then" and see how that works.

0
Thank you so much!!! I just realized I was adding the player to the table multiple times, because a string (player.Name) is not the same as a player (r) JoshChubi 74 — 4y
0
Yup, just a simple logic error. Np. CeramicTile 847 — 4y
Ad
Log in to vote
0
Answered by 4 years ago
local function giveMoney()
    local partsInRegion = workspace:FindPartsInRegion3(vaultRegion,nil,math.huge)
    local giveTo = {}

    for _,part in pairs(partsInRegion) do
        local player = game.Players:GetPlayerFromCharacter(part.Parent)
        if player then

            local playerFound = false
            for _,r in pairs(giveTo) do
                if r == player.Name then
                    playerFound = true
                end
            end

            if not playerFound then
                table.insert(giveTo,player)
            end
        end
    end
    for _,player in pairs(giveTo) do
        local newAmount = player.leaderstats.CurBagAmount.Value + 5
        if newAmount > player.leaderstats.BagSize.Value then
            newAmount = player.leaderstats.BagSize.Value
        end
        player.leaderstats.CurBagAmount.Value = newAmount
    end
end
1
Change line 11 to "if r == player then" and see how that works. CeramicTile 847 — 4y
0
You could've have edited your question and included your code there, instead of posting it as an answer. ScuffedAI 435 — 4y
0
THATS THE ANSWER CERAMICTILE, post it as answer so i can accept it JoshChubi 74 — 4y

Answer this question