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

How do you assign a saving variable to a player?

Ok. So, I understand this as a very basic idea of assigning a variable, but I want to be able to have the ability to update and save. I've read up on the DataStoreService and I am just confused on how I do this in reality for my script. I just want to be able to store a numerical value.

So far, I have basically this. I need help with the logic of the pathway to storing it.

 game.Players.PlayerAdded:connect(function(player)
	-- This is where I am stuck, I don't know where to store the variable, but I believe the code for it would go here, initialized to 1, not 0.
	print (player.name .. "Has entered the server.") -- Announces a person joining, and to check if its worked this far
end)

for _, player in pairs(game.Players:GetPlayers()) do
	PlayerAdded(player) 
end 

Please help, Thanks.

1 answer

✓ Accepted answer

game:GetService("DataStoreService"):GetDataStore("RandomNumbers"):SetAsync(player.Name, math.random(1,10)) -- Sets the value of the key named as the player's name to any random number between 1 and 10 within the DataStore named "RandomNumbers"

print(game:GetService("DataStoreService"):GetDataStore("RandomNumbers"):GetAsync(player.Name) -- Prints the value of the key named as the player's name within the DataStore named "RandomNumbers".

A DataStore is a.. Group of Keys. A key can hold any type of Value, it can be numbers, booleans, strings, or Lua tables.

1 comment

0

Thanks, I think this will work.

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