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

How can I use "player" in a connection line of a regular script?

I'm trying to work on scripting a save stats feature, and I want the players' stats to save when their stats change. The only problem is that I don't know how to use "Player" when in a connection line of a regular script, not Local Script. Here's the save script, if you need the code:

function saveStats(Player)
	repeat wait() until Player
	--gold and gems save
	Player:SaveNumber(goldKey, Player.leaderstats.Gold.Value)
	Player:SaveNumber(gemsKey, Player.leaderstats.Gems.Value)
	--level and xp save
	Player:SaveNumber(levelKey, Player.leaderstats.Level.Value)
	Player:SaveNumber(xpKey, Player.Exp.Value)
end

How would a connection line work? Also, if you can find a way to make this in a LocalScript feel free, just nothing has been working for me in LocalScripts.

2 answers

This all depends on what triggers this.


Is this just something running constantly? Every 10 seconds or so? In that case, you can just loop over players yourself and call the function:

while wait(10) do
	for _, player in pairs(game.Players:GetPlayers()) do
		saveStats(player);
	end
end

To do it when they leave, you can just use PlayerRemoving event:

game.Players.PlayerRemoving:connect( saveStats );

Or a mixture of the two. Or other triggers, like from a Touch event on a part:

function saveOnTouch(hit)
	if hit.Parent then
		local character = hit.Parent;
		local player = game.Players:GetPlayerFromCharacter(character);
		if player then
			saveStats(player);
		end
	end
end
somePart.Touched:connect( saveOnTouch );

To save on change, you would use an anonymous function in what is called a closure. This is also how Chatted events are usually done.

someValue.Changed:connect(function() saveStats(someValue.Parent) end);

(Where someValue is a child of a player that you are tracking changes on to save stats)


Please note that you should remove the repeat loop from your function. Since it's a repeat loop, and not a while loop, it will always wait at least once (which is bad) and since player is a parameter, it can't actually change so the line doesn't accomplish anything.

2 comments

1

I want it so that when the stats are changed, it runs the function, like the example I made. I'm just not sure how to connect it to the player.

0

Sorry, I missed that -- my post now addresses that

Since BlueTaslem didn't address your comment, I'll add this answer.

As Data Persistence is not exactly deprecated, I highly suggest learning to use DataStores. They give two main benefits over Data Persistence: you can save whatever you want, as long as it isn't pure ROBLOX Objects (i.e. not serialized.), and you can access the saved data whenever you want, unlike DP which requires the Player be there.

Anyway, since you are creating the Value objects you are saving the Values of somewhere, I suggest using the Changed event on the Value objects, and saving the data for that specific Value object there.

Here's an example of that:

game.Players.PlayerAdded:connect(function(Player)
	local stats = Instance.new("IntValue", Player)
	stats.Name = "leaderstats"

	local gold = Instance.new("IntValue", stats)
	gold.Name = "Gold"
	gold.Changed:connect(function()
		Player:SaveNumber(goldKey, gold.Value)
	end)
	--And so on for the other three stats.
end)

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