Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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.

5 comments

0

What is the "break" for?

0

Also, I might be able to work out what's going on if you provide the GiveMoneyToPlayers() function.

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.

0

to exit the loop, i forgot to add after the loop there is a cleanup() function

0

@Quezzert Just posted the giveMoney() function

2 answers

✓ Accepted answer

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

2 comments

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)

0

Yup, just a simple logic error. Np.

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

3 comments

1

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

0

You could've have edited your question and included your code there, instead of posting it as an answer.

0

THATS THE ANSWER CERAMICTILE, post it as answer so i can accept it

This discussion is preserved as it was. New answers, comments, and votes are closed.