Im trying to give all players rewards but only 1 gets it

Players = game:GetService("Players")
 TeleportService = game:GetService("TeleportService")
cool = false
store = game:GetService("ReplicatedStorage")
script.kills.Value = 9
script.unit.Value = 3


while wait(1)do
	print(print(#game.Players:GetPlayers()))
	if script.timer.Value < 1 then
	for _,player in ipairs(game.Players:GetPlayers()) do
			gold = player.unit.cash.Value
			win = player.unit.win.Value
			money = player.stats.Money.Value
			vip = player.unit.gp.Value
			coins = player.unit.gp2.Value
			star = player.unit.gp3.Value
			exp = player.stats.Exp.Value
	local wave = script.wave.Value
	local kills = script.kills.Value
	local timer = script.timer.Value
			if cool == false then
				unit = script.unit.Value
		cool = true
			if wave >= 25 then
				script.kills.Value = 0
					print("you win noob")
					wait(1)
					script.Parent.units.gate:Destroy()
					if win == "no" then
						player.unit.win.Value = "yes"
					if vip < 1 and coins < 1 then
						player.stats.Money.Value = money + 70 * 3
						
					end
					if vip >= 1 and coins >= 1 then
						player.stats.Money.Value = money + 70 * 9
						
					end
					if vip < 1 and coins >= 1 then
						player.stats.Money.Value = money + 70 * 6
						
					end
					if vip >= 1 and coins < 1 then
						player.stats.Money.Value = money + 70 * 6
					
					end
					if vip < 1 and star < 1 then
						local rand = math.random(1,80)
						if rand == 80 then
							player.stats.Exp.Value = exp + 1
							end
					end
					if vip >= 1 and star < 1 then
						local rand = math.random(1,60)
						if rand == 60 then
							player.stats.Exp.Value = exp + 1
						end
					end
					if vip < 1 and star >= 1 then
						local rand = math.random(1,60)
						if rand == 60 then
							player.stats.Exp.Value = exp + 1
						end
					end
					if vip >= 1 and star >= 1 then
						local rand = math.random(1,19)
						if rand == 19 then
							player.stats.Exp.Value = exp + 1
						end
						end
						
					end
					
					wait(3)
					TeleportService:TeleportAsync(15018028887, {player})
					
										
					
					
					
				
				elseif kills > 0 and unit > 0 then
					
					if wave < 10 then
						script.unit.Value = unit -1
					clone = store.enemy.toilet1:Clone()
					clone.Parent = game.Workspace	
					local currentPivot = clone:GetPivot()
					clone:PivotTo(currentPivot * CFrame.new(game.Workspace:WaitForChild("spawn1").Position))
					clone1 = store.enemy.glass1:Clone()
					clone1.Parent = game.Workspace	
					local currentPivot = clone1:GetPivot()
					clone1:PivotTo(currentPivot * CFrame.new(game.Workspace:WaitForChild("spawn2").Position))
					clone2 = store.enemy.glass1:Clone()
					clone2.Parent = game.Workspace	
					local currentPivot = clone2:GetPivot()
					clone2:PivotTo(currentPivot * CFrame.new(game.Workspace:WaitForChild("spawn3").Position))
					end
					if wave >= 10 then
						script.unit.Value = unit -1
						clone = store.enemy.toilet2:Clone()
						clone.Parent = game.Workspace	
						local currentPivot = clone:GetPivot()
						clone:PivotTo(currentPivot * CFrame.new(game.Workspace:WaitForChild("spawn1").Position))
						clone1 = store.enemy.glass1:Clone()
						clone1.Parent = game.Workspace	
						local currentPivot = clone1:GetPivot()
						clone1:PivotTo(currentPivot * CFrame.new(game.Workspace:WaitForChild("spawn2").Position))
						clone2 = store.enemy.glass1:Clone()
						clone2.Parent = game.Workspace	
						local currentPivot = clone2:GetPivot()
						clone2:PivotTo(currentPivot * CFrame.new(game.Workspace:WaitForChild("spawn3").Position))
					end
				else
					if kills < 1 then
					wait(5)
					script.unit.Value = 4
			script.wave.Value = wave + 1			
				script.kills.Value = 10 
					player.unit.cash.Value = gold + 100 + wave*2
					wait(5)
					
			end
		end
		
		
		
	end
	
end
	end wait(7)
	cool = false
end```

The thing is, only 1 player earns coins at the end of round, weird.

what exactly is going on here?

if vip < 1 and coins < 1 then
  player.stats.Money.Value = money + 70 * 3
end
if vip >= 1 and coins >= 1 then
  player.stats.Money.Value = money + 70 * 9
end
if vip < 1 and coins >= 1 then
  player.stats.Money.Value = money + 70 * 6
end
if vip >= 1 and coins < 1 then
  player.stats.Money.Value = money + 70 * 6
end
if vip < 1 and star < 1 then
  local rand = math.random(1,80)
  if rand == 80 then
    player.stats.Exp.Value = exp + 1
  end
end
if vip >= 1 and star < 1 then
  local rand = math.random(1,60)
  if rand == 60 then
    player.stats.Exp.Value = exp + 1
  end
end
if vip < 1 and star >= 1 then
  local rand = math.random(1,60)
  if rand == 60 then
    player.stats.Exp.Value = exp + 1
  end
end
if vip >= 1 and star >= 1 then
  local rand = math.random(1,19)
  if rand == 19 then
    player.stats.Exp.Value = exp + 1
  end
end

the problem is only 1 player receive reward or get teleported, ik about the issue of not ussing teleportpartyasync, i was experimenting, since its not working either.

i’m more concerned about how ugly the code is i could help clean it

If you’re testing this in studio, this may be because you’re using the TeleportService. If you’re doing things in studio that can only be done on the server, you should use the IsStudio method from RunService to determine if the client is running through Roblox Studio or the Roblox Game Client.

yah my code is a mess :D. i was thinking if i should get all players in a table or something? to fix the issue?

its not in studio i use alts to test

The only issue is that one player is getting bonus coins each round, only 1 player gets game reward. The teleportation ik i should use teleportpartyasync but since its not working i was thinking about looping and maybe players gets teleported 1 by 1 idk :D, because same effect is with partyasync or teleporttoprivateserver

Yes, you should collect the players you’d like to teleport into a table. Just check if the players meet the criteria, add them to the table, and then use TeleportAsync. Keep in mind that this function yields, so if your loop is not continuing fast enough, then it’s because TeleportAsync is being called inside of each iteration between players, which can take up time.

local placeId = 0 -- Replace with your place ID.
local playersToTeleport = {}

for _, player in pairs(Players:GetPlayers()) do
    if meetsCriteria then
        table.insert(playersToTeleport, player)
    end
end

TeleportService:TeleportAsync(placeId, playersToTeleport)

i see, so i should always call the players in a table, for rewards or teleport.

You can bundle the players together into an array to which you want to do an action for. If you want to reward the players it’s up to you on how you want to write that implementation. The TeleportAsync method requires that the second argument be an array of players so you should always bundle up the players you are going to teleport before making a teleport request.

1 Like

teleport i understand, but how should i reward all at once?

Based on your value system where you store the Money object under stats and etc, I’d say create a function that takes a player as an argument and gives the player a reward based on either the condition given to the function or the specific values to add to the balance of the player. For example, you can do something like this:

--- Use this function as so:
--- pcall(giveMoneyToPlayer, player, amountOfMoney)
--- ^^^^^
--- This method (pcall) will ignore errors.

local function giveMoneyToPlayer(player, amountOfMoney)
	player.stats.Money += amountOfMoney
end

local function givePlayerReward(player: Player, rewardType: string): ()
	if typeof(player) ~= 'Instance' or not player:IsA('Player') then
		error(string.format("Invalid argument type for 'player'. Expected Player, got%s %s.", player:IsA('Player') and " (an instance of Player)" or "", type(player)), 2)
	elseif type(rewardType) ~= 'string' then
		error(string.format("Invalid argument type for 'rewardType'. Expected string, got %s.", type(rewardType)), 2)
	else
		local rewardTypeFormatted = rewardType:lower()

		if rewardTypeFormatted == 'general' then -- If the reward is not categorized.
			-- give them money
			local success, response = pcall(giveMoneyToPlayer, player, 100)
			
			if success ~= true then
				print('We could not reward the player named ' .. player.Name .. '!' .. 'Below is the reason why... \n' .. response)
			end
		elseif rewardTypeFormatted == 'special' then
			-- do something special here
		end
	end
end

While this may not explicity work for you, you are welcome to test it for yourself in your code as I have done my best to adapt to your code’s environment.

There are some concepts in here you may not have heard of but I hope the comment I have provided upon pcall is descriptive enough. If it isn’t, here’s the documentation:

Here’s a usage example:

local playerList = Players:GetPlayers()
local randomPlayer = playerList[math.random(1, #playerList)]

if typeof(randomPlayer) == 'Instance' then
    givePlayerReward(randomPlayer, 'general')
end

You can always change the way your reward system works. If you don’t want named rewards, you can simply just use the giveMoneyToPlayer function with the pcall method instead.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.