Transferring player's int value to team's int value when they leave the game

In my game you work as a team to collect Fuel Cells into your inventory and then dispense them into a Fuel Cell Container that totals them all up. Once all 50 Fuel Cells are inside of the Fuel Cell Container the players win. However, there are exactly/only 50 Fuel Cells in the game, so if a player leaves when they have some Fuel Cells in their inventory then that makes it literally impossible for the other players to win since they need ALL 50 of the Fuel Cells, and the fuel cells will be deleted once the given player leaves the game. I have no clue how to combat this and i can’t find anything online. Any help would be greatly appreciated! :slight_smile: and sorry i don’t have any code right now, i genuinely don’t know what this code would look like. (btw the Fuel Cells inside of the individual players’ inventories are saved as an int value inside of StarterGui, and the Total Fuel [which is another int value that is inside of Server Storage] is what i need their int value to be added to).

1 Like

Well this seems like an issue with your system as a whole. Why does every player have their own value instance instead of the team having one? Unless you give a random player from the team the instance of the player that left I don’t see how you could fix this.

Like @DEVLocalPlayer said, this is really inefficient. However, if you really want to stick with this system, just make them transfer when the player leaves.

--server-side script
local players = game:GetService("Players")
local teamFuel = --team fuel location here

local function transfer(player)
    local playerFuel = --location of player's individual fuel here
    teamFuel.Value += playerFuel.Value
end

players.PlayerRemoving:Connect(transfer)

By the way if the player’s fuel value is in StarterGui before you start testing (e.g. you put it in there and it is not added there via a script) then look for that value in the player’s PlayerGui instead of StarterGui.

4 Likes

Will this script only transfer the player that left’s value or everyone’s value? (i’m specifically looking for it to only transfer the player that left’s value).
Also, the reason i’m doing it like this is for game play reasons. I found that it felt too game-y to have them collect the fuel cells and for them to magically go into the total fuel, so i made it so the players can only hold a maximum of ten fuel cells at a time before they have to drop them off into a Fuel Cell Container that will transfer them to the Total Fuel as a percentage (each of the fuel cells is worth two percent, and the players need to get the total fuel to reach one hundred percent to progress).

1 Like

The script I sent will only transfer the fuel of the player that left, and it will transfer it to the team fuel. The PlayerRemoving event passes a parameter, which is the player that left the game. The playerFuel variable should be used to get the player’s fuel value, and then the line below that should add it to the team’s total fuel. I hope this helps!

It didn’t work for some reason! Here’s the current code: (the script is inside of ServerScriptService)

local Players = game:GetService("Players")
local FuelPercent = game.ServerStorage.Fuel

local function transfer(Player)
	
	local FuelCells = Player.PlayerGui.FuelCellCount.FuelCells
	
	if FuelCells.Value > 0 then
		FuelPercent.Value = FuelPercent.Value + FuelCells.Value * 2
		FuelCells.Value = 0
	end
end

Players.PlayerRemoving:Connect(transfer)

Anything in the output (errors/warnings)? If not, try adding a BindToClose() function, which runs any provided code before the game shuts down.

game:BindToClose(function()
    if game["Run Service"]:IsStudio() then task.wait(3) return end
    for _, player in pairs(game.Players:GetPlayers()) do
        transfer(player)
    end
    task.wait(3)
end)
1 Like

New current code:

local Players = game:GetService("Players")
local FuelPercent = game.ServerStorage.Fuel

local function transfer(Player)

	local FuelCells = Player.PlayerGui.FuelCellCount.FuelCells

	if FuelCells.Value > 0 then
		FuelPercent.Value = FuelPercent.Value + FuelCells.Value * 2
		FuelCells.Value = 0
	end
end

Players.PlayerRemoving:Connect(transfer)

game:BindToClose(function()
	if game["Run Service"]:IsStudio() then task.wait(3) return end
	for _, Player in pairs(game.Players:GetPlayers()) do
		transfer(Player)
	end
	task.wait(3)
end)

Output: PlayerGui is not a valid member of Player “Players.tkbergs”

Can you send the script which that error came from?

1 Like

The error came from line 6 of that script i just sent you.
The “local FuelCells = Player.PlayerGui.FuelCellCount.FuelCells” line is where the error is occurring.

I would not recommend having the value in PlayerGui. I’m pretty sure PlayerGui is destroyed even before PlayerRemoving is fired. Try making the value under the player’s Player object.

That’s what i was just thinking. I’m making a another script that will create the “Fuel Cells” int value for each player. What should the parent be for the int value? I tried making it the player’s Character but now the script that is inside of the individual Fuel Cells aren’t adding to the int value.
Here is the Output: Workspace.Mechanics.Fuel Cells.FuelCell.Part.+1 Fuel:3: attempt to index nil with ‘Value’

Use a script that adds it to the player’s Player object in the Players service.

I’ve changed it so that the Fuel Cells int value is created under the Player object in the Players service, however the script still isn’t transferring the value to the Total Fuel when they leave the game. Does it only work on Roblox and not studio? because i’ve only been trying this in Studio since i’ve been trying to not save it until i get it to work.

Are there any errors in the output?

WAIT HOLD UP! IT WORKS!!!
I was testing it wrong. it works when i test it with two players on Studio now. I’m going to also test it on Roblox in a second. However, i have just a quick question about a part of the script!

There are two “task.wait(3)” parts of the code, and i’m wondering if i need both or if they can be less than 3 seconds? or if they do both need to be 3 seconds, how come? just curious! :smiley:

game:BindToClose(function()
	if game["Run Service"]:IsStudio() then task.wait(3) return end
	for _, Player in pairs(game.Players:GetPlayers()) do
		transfer(Player)
	end
	task.wait(3)
end)
1 Like

Create a connection for when a player leaves and respawn all Fuel Cells that they had in their inventory.

All those do is just make sure the game has enough time to save the data. You can change them, just make sure the game still has enough time to save the data!

1 Like

Okay! i’ll leave them because i think they’re good. Thank you so much for your help btw!!! :smiley:

I’ll put the final code here for future reference:

local Players = game:GetService("Players")
local FuelPercent = game.ServerStorage.Fuel

local function transfer(Player)

	local FuelCells = Player.FuelCells

	if FuelCells.Value > 0 then
		FuelPercent.Value = FuelPercent.Value + FuelCells.Value * 2
		FuelCells.Value = 0
	end
end

Players.PlayerRemoving:Connect(transfer)

game:BindToClose(function()
	
	if game["Run Service"]:IsStudio() then
		task.wait(3) return end
	for _, Player in pairs(game.Players:GetPlayers()) do
		transfer(Player)
	end
	task.wait(3)
end)
1 Like

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