How to save individual player data in server script?

I’m trying to create a script that can detect when a user dies in a round and then later remember that the user died in a server script.

I have a working script that can detect when a user died in a for loop but I do not know how to carry this data outside of that loop and use it later.

I’ve scowered the forum so far but I could not find any individual post that worked for what I was trying to do. I have tried a LocalScript that can detect when the user dies and changes a BoolValue to true or false but the player is reset when it dies so the BoolValue would never change to true when the player died. I also don’t want to rely on the client for information due to explotiers. What I think will work is checking when the player dies in the for loop and then outside of that for loop using that information again to see if the player died during a round.

Previously mentioned snippet of server script:

for timeleft = 30, 0, -1 do
		for i,v in pairs(Players:GetChildren()) do
			if v.PlayerGui:FindFirstChild("ScreenGui") ~= nil then
				v.PlayerGui.ScreenGui.Broadcast.Text = "Round Time Left: " .. timeleft
				if v.Character.Humanoid.Health == 0 then
					print("Player died.")
					-- This is where I am having trouble. How do I carry this information over to when the round ends?
				end
			end
		end
		wait(1)
	end
	
	-- Check for plr death
	
	for i,v in pairs(Players:GetChildren()) do
		print(v.Name)
		if v.PlayerGui:FindFirstChild("ScreenGui") ~= nil --[[insert check if player died during round here]] then
			v.PlayerGui.ScreenGui.SmallText.Text = "You survived!"
		else
			v.PlayerGui.ScreenGui.SmallText.Text = "You died. :("
		end
		v = true
	end

In short, how do I carry a value outside of a for loop and save it for an individual player in a server script? Also, is there a better way to go about this?

Well if it’s just being used in that one server you can store it in a table as that will be kept in the script but only for that server.

Also you can you Humanoid.Died instead of Humanoid.Health == 0

So something like:

v.Character.Humanoid.Died:Connect(function()
 table.insert(Died,--value you want)
end)
1 Like

I will try this and update you in a bit. I am a bit busy due to Easter atm.

1 Like

Would that function still work as intended in the for loop?

Keep the table outside the loop but make sure you clear it when the loop finishes

1 Like

That’s what I did. It worked, thanks.

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