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!
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).
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.
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).
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)
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?
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! ![]()
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)
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!
Okay! iâll leave them because i think theyâre good. Thank you so much for your help btw!!! ![]()
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)
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.