I am creating a datastore for a game I am creating with a group. The script I am using is very basic, so I will be sharing it here for you to see, but it does not seem to be working properly. API access is enabled, I hope you can help me!
–[DEFINING DATASTORE]---------------------------------
local DataStore = DSS:GetDataStore(“Data”)
–[CORE SCRIPT]---------------------------------
game.Players.PlayerAdded:Connect(function(Player)
local Folder = Instance.new(“Folder”)
Folder.Name = “leaderstats”
Folder.Parent = Player
local Coins = Instance.new("IntValue")
Coins.Name = "Coins"
Coins.Parent = Folder
local Flex = Instance.new("IntValue")
Flex.Name = "Flex"
Flex.Parent = Folder
local data
local flex
local success, errormessage = pcall(function()
data = DataStore:GetAsync(Player.UserId.."-Coins")
flex = DataStore:GetAsync(Player.UserId.."-Flex")
end)
if success then
Coins.Value = data
Flex.Value = flex
else
print("There was an error while saving")
warn(errormessage)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, errormessage = pcall(function()
DataStore:SetAsync(player.UserId…“-Coins”,player.leaderstats.Coins.Value)
DataStore:SetAsync(player.UserId…“-Flex”,player.leaderstats.Flex.Value)
end)
if success then
print("Player Data saved successfully")
else
print("error when saving data")
warn(errormessage)
end
end)
Thank you so much for any help, I hope it’s an easy fix.
Hm… when I am in the game the data will not save/load. I am unsure which it is not doing, but when you leave and rejoin your data is not the same as it was. Possibly the game isn’t updating?
Maybe try changing your defining datastore “Data” to something else, if you have more than one of these with the same saving name in the game it would break it.
Are you using LocalScripts (Client) or Scripts (Server) to update the values? If you’re using a LocalScript, you’ll need to use RemoteEvents to tell a Script on the server to update the values.
Why is it a local script? Data stores only work towards the server, I recommend putting it in serverscriptservice as a (server)script. Make sure you define everything correctly too, and keep your output out so it can help you towards fixing things.
Judging by the fact that the saving script isn’t actually erroring, the save script itself is probably on the server. I believe the issue is that they’re using ContextActionService or UserInputService to decide when to update the values, which can only be done in a LocalScript, while the values can only be properly updated on the server. They’ll need to add some form of communication via RemoteEvents to tell the server to update the values.
I did this, but it doesn’t seem to work. Basically I replaced the part of the script that added one to the flex from the local script into the script to fire the server,
and on the script in server script service I wrote this
No, don’t do that. Don’t EVER give the client any power. Don’t even suggest that, because that is incorrect and risky. We don’t want beginner scripters looking for a quick fix to their problem to see that, and just do that until they learn better.
Well that won’t work, because the you increase flex value when the key e is pressed, which also toggles and animation so it needs to be under starter character