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

How to give players through the server money at the end of each wave?

Hey! I've been trying to make td game for a while now and my friends keep telling me to implement money after each round. I've tried and tried and since my money system runs on the server, not the client, I can't just say game.Players.LocalPlayer.Gold.Value += 200. This is very annoying and I need someone to help please! I'll leave all my code down below.

The serverscript handling the money:

game.ReplicatedStorage.Events.GiverCash.OnServerEvent:Connect(function()
	for i,de in ipairs(game.Players:GetChildren()) do
		if game.Workspace.Ready.Value == true then
			de.Gold.Value += 200
			game.Workspace.Ready.Value = false
		else
			warn("Ready value isn't true")
		end
	end
end)

The localscript firing the event

while true do
	wait(0.1)
	if workspace.Ready.Value == true then
		game.ReplicatedStorage.Events.GiverCash:FireServer(game.Players)
	end
end

The serverscript handling the round system and setting the value to true after each round:

local SS = game:GetService("ServerStorage")


local mob = require(script.Mob)
local tower = require(script.Tower)
local map = workspace.Baselands

local info = workspace.Info
local gameOver = false

map.Base.Humanoid.HealthChanged:Connect(function(health)
	if health <= 0 then
		gameOver = true
	end
end)



wait(5)
for i=15, 0, -1 do
	info.Message.Value = "Game starting in..." .. i
	task.wait(1)
end

for wave=1, 15 do
	info.Wave.Value = wave
	info.Message.Value = ""
	
	workspace.Ready.Value = true
	
	if wave <= 2 then
		mob.Spawn("Zombie", 3 * wave, map)
	elseif wave <= 4 then
		for i=1, 3 do
			mob.Spawn("Zombie", wave, map)
			mob.Spawn("Noob", wave, map)
		end
	elseif wave <= 7 then
		for i=1, 3 do
			mob.Spawn("Zombie", wave, map)
			mob.Spawn("Noob", wave, map)
			mob.Spawn("Mech", 2 * (wave * 0.5), map)
		end
	elseif wave <= 9 then
		mob.Spawn("Mech", 2 * wave, map)
		mob.Spawn("Noob", wave, map)
		for i=1, 2 do
			mob.Spawn("Zombie", (wave * 0.5), map)
			mob.Spawn("Mech", (wave * 0.5), map)
		end
		mob.Spawn("Teddy", 1, map)
	elseif wave <= 10 then
		mob.Spawn("Zombie", 40, map)
		for i=1, 2 do 
			mob.Spawn("Zombie", 2, map)
			mob.Spawn("Mech", 5, map)
			mob.Spawn("Teddy", 1, map)
		end
		mob.Spawn("Teddy", 10, map)
	elseif wave <= 11 then
		for i=1, 3 do
			mob.Spawn("Shock", wave, map)
			mob.Spawn("Teddy", wave, map)
			mob.Spawn("Mech", wave, map)
		end
	elseif wave <= 12 then
		for i=1, 2 do
		mob.Spawn("Shock", 2 * wave, map)
		mob.Spawn("HardShock", 7, map)
			mob.Spawn("Teddy", 3 * wave, map)
		end
	elseif wave <= 14 then
		for i=1, 2 do
			mob.Spawn("Shock", 2 * wave, map)
			mob.Spawn("HardShock", 7, map)
			mob.Spawn("Tedyy", 10, map)
			mob.Spawn("Molten", 5, map)
		end
	elseif wave == 15 then
		mob.Spawn("Shock", 5 * wave, map)
		mob.Spawn("HardShock", 2 * wave, map)
		mob.Spawn("Molten", 10, map)
		mob.Spawn("SpeedyMolten", 4, map)
		mob.Spawn("MoltenBoss", 1, map)
	end
	
	repeat
		task.wait(1)
	until #workspace.Mobs:GetChildren() == 0 or gameOver
	
	if gameOver then
		info.Message.Value = "Game Over"
		break
	end
	
	for i=5, 0, -1 do
		info.Message.Value = "Next wave starting in..." .. i
		task.wait(1)
	end
end

1 answer

✓ Accepted answer
game.ReplicatedStorage.Events.GiverCash.OnServerEvent:Connect(function(plr)
        if game.Workspace.Ready.Value == true then
            for _, plr in pairs(game.Players:GetChildren()) do
                   plr.Gold.Value = plr.Gold.Value + 200
            end 
            game.Workspace.Ready.Value = false
        else
            warn("Ready value isn't true")
        end
    end
end)

Maybe this would work for you better, but I'm not sure what's wrong with the script in the first place.

3 comments

0

Nope, still didn't work. There isn't any errors but it's only giving one player the cash. I did a test in studios and it gave only one person cash.

0

Nope, still didn't work. There isn't any errors but it's only giving one player the cash. I did a test in studios and it gave only one person cash.

0

I changed the answer, try this, also know I wrote it in the scriptinghelpers editor, so it's not the greatest.

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