DataStore Not Working (Group Game)

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!

The Script
--[DEFINING VARIABLES]---------------------------------

local DSS = game:GetService(“DataStoreService”)

–[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.

After testing, the script seemed to load and save fine.
What’s it not doing that it should be?

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?

Is it printing the “Player data saved successfully” message when you leave?

Yes, it is

30chars30chars30chars

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.

local DataStore = DSS:GetDataStore(“NewData123”)

When you update the Coins and Flex IntValues, are you doing so on the server or client? It’ll only save changes made on the server.

How do I check that? I am new to scripting, and someone else scripted a lot of this. In the script Flex is increased when E is pressed (using Enums)

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.

It’s a local script. How can I fire a remote event to update the value?

i advise you update the data on the server doing it on client can make your game vulnerable to exploits.

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.

Ok then, that would make sense, so I would go with what you are announcing to him.

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

local Player = game.Players.LocalPlayer

local Flex = Player.leaderstats.Flex.Value

game.ReplicatedStorage.Flex.OnServerEvent:Connect(function(player)

Flex.Value = Flex.Value + 1

end)

Edit – figured it out. Your example script actually worked.

Here’s some test code using DS2, it’s a lot easier to use and I suggest getting used to it.

Sorry if this doesn’t solve your problem.


local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PlayerData = require(ReplicatedStorage.PlayerData)
local MainKey = "NY_"

local Rounds = require(script.RoundHandler)

PlayerData.Combine(MainKey, "Stats", "Misc")

local function SetDefault()
	
	local Dictionary = {
		
		Stats = {
			
			["Cash"] = 0;
			["Wins"] = 0;	
			
		},
		Misc = {
			
			["MetTheOwner"] = false;	
			["AlphaTester"] = false;
			
		},
		
	}
	return Dictionary
	
end

game.Players.PlayerAdded:Connect(function(Player)
	
	local UserData = PlayerData(MainKey, Player):Get(SetDefault())
	
	--[[ STATS ]]--
	
	local PlrStats = Instance.new("Folder", Player)
	PlrStats.Name = "leaderstats"
	
	local Cash = Instance.new("IntValue", PlrStats)
	Cash.Name = "Cash"
	
	local Wins = Instance.new("IntValue", PlrStats)
	Wins.Name = "Wins"
	
	--[[ AWARDS/MISC ]]--
	
	local MiscFolder = Instance.new("Folder", Player)
	MiscFolder.Name = "Misc"
	
	local MetTheOwner = Instance.new("BoolValue", MiscFolder)
	MetTheOwner.Name = "MetTheOwner"
	
	local AlphaTester = Instance.new("BoolValue", MiscFolder)
	AlphaTester.Name = "AlphaTester"
	
	--[[ LOADING/SAVING ]]--
	
	local StatsSave = PlayerData("Stats", Player)
	local MiscSave = PlayerData("Misc", Player)
	
	local function UpdateStats(New)
		Cash.Value = StatsSave:Get(New).Cash
		Wins.Value = StatsSave:Get(New).Wins
	end
	
	local function UpdateMisc(New)
		MetTheOwner.Value = MiscSave:Get(New).MetTheOwner
		AlphaTester.Value = MiscSave:Get(New).AlphaTester
	end
	
	--[[ UPDATING ]]--
	
	UpdateStats(UserData.Stats)
	UpdateMisc(UserData.Misc)
	
	StatsSave:OnUpdate(UpdateStats)
	MiscSave:OnUpdate(UpdateMisc)
	
	--spawn(function()
		--while(true)do
			--UserData.Stats.Cash = UserData.Stats.Cash + 5
			--StatsSave:Set(UserData.Stats)
			--if UserData.Misc.AlphaTester == false then
				--UserData.Misc.AlphaTester = true
				--MiscSave:Set(UserData.Misc)
				--print(UserData.Misc.AlphaTester)
			--end
			--wait(1)
		--end
	--end)
	
	--[[ ETC ]]--
	
end)

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.

2 Likes

Okay… so how can I create a script that will update the servers values without giving the client power…

Follow what @RainScripts said, except probably don’t put DataStore2 in ReplicatedStorage and don’t do Instance.new(ClassName, Parent).

1 Like

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