Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do you assign a saving variable to a player?

Asked by 10 years ago

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

Log in to vote
0
Answered by 10 years ago

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.

0
Thanks, I think this will work. rider5376 5 — 10y
Ad

Answer this question