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

How to use DataStore with FilteringEnabled?

local DataStore = game:GetService("DataStoreService")
local killsds = DataStore:GetDataStore("Kills")

game.Players.PlayerAdded:connect(function(player)
	local stats = Instance.new("Folder",player)
	stats.Name = "Stats" -- creates a leaderboard

	-- CREATES KILLS VALUE --
	local killsvalue = Instance.new("NumberValue", stats)
	
	killsvalue.Name = "Kills" -- creates a leaderstat value
	killsvalue.Value = killsds:GetAsync(player.UserId) or 0 -- gets past value
	killsds:SetAsync(player.UserId,killsvalue.Value) -- sets a new value
	
	-- LEAVE BACKUP SAVE --
	game.Players.PlayerRemoving:connect(function(player)
		print("Saving Data for user "..player.UserId)
		killsds:SetAsync(player.UserId,player.Stats.Kills.Value)
		print(player.UserId.."'s Data has been saved successfully.")
		if error then
			print("DATA ERROR: "..player.UserId.."'s Data has been saved unsuccessfully")
		end
	end)
end)

I know you have to do something with a RemoteFunction but I'm not exactly sure what to do. Help is much appreciated.

6 comments

0

Can you specifically explain your problem?

0

The script works when FilteringEnabled is false, not when it's true. I converted my entire game other than this. I dont know what else you need to know.

0

Datastore only works in server scripts.

0

Thats what I'm using @cabbler

0

@SchonXIV, what exactly is/isn't working? Are there errors?

0

@chess123mate There's no errors, and the value doesn't save. The folder and value shows up.

1 answer

You connected the events a little wrong. To put it simply, this is how your code is:

local DataStore = game:GetService("DataStoreService")
local killsds = DataStore:GetDataStore("Kills")

game.Players.PlayerAdded:connect(function(player)
    --Execute
    game.Players.PlayerRemoving:connect(function(player)
        --execute
    end)
end)

To put it simply, you can't connect it this way. It should be:

local DataStore = game:GetService("DataStoreService")
local killsds = DataStore:GetDataStore("Kills")

game.Players.PlayerAdded:connect(function(player)
    --Execute
    
end)
game.Players.PlayerRemoving:connect(function(player)
	--execute
end)

Now, there are a few things to note: First, the player removing event will Not fire in time if the last person is leaving the server, or if the server is shutdown for whatever reason, as the game is closing. Secondly, you have some un required code. If the only thing you want to save is kills, then the final script should look like:

local DataStore = game:GetService("DataStoreService")
local killsds = DataStore:GetDataStore("Kills")

game.Players.PlayerAdded:Connect(function(player) -- connect must be Connect btw
    local stats = Instance.new("Folder",player)
    stats.Name = "Stats" -- creates a leaderboard
    local killsvalue = Instance.new("NumberValue", stats)
    killsvalue.Name = "Kills" -- creates a leaderstat value
    killsvalue.Value = killsds:GetAsync(player.UserId) or 0 -- gets past 
end)
game.Players.PlayerRemoving:Connect(function(player)
	killsds:SetAsync(player.UserId,player.Stats.Kills.Value)
end)
game:BindToClose(function() -- if the last player in the game leaves, don't close the server down until it's saved
	local p = game.Players:GetPlayers()
	for a=1,#p  do
		local player = p[a]
		killsds:SetAsync(player.UserId,player.Stats.Kills.Value)
	end
end)

7 comments

0

I appreciate the edit, but the problem is that FilteringEnabled breaks it.

0

This is a normal script under workspace or something, correct? There should be no issue as it is server sided.

0

I had it in ServerScriptService and it didn't work. Switched it to workspace and didn't work either.

0

A proper DataStore script also handles errors (since SetAsync could fail temporarily and then you'd lose that player's progress). Also, I suspect that when BindToClose is called, there won't be any players - you should instead wait until there are no more PlayerRemoving functions running SetAsync (you'd need a variable like 'numSaving = 0' to start with).

0

@Chess; Yes, a proper script would include the pcall function in case of the SetAsyn returning an error. As for the BindToClose, it does work without the need of the wait, as I have that set up in my game which has some people playing. Either way I simply put this to help him understand the correct places to connect whatever. Schon, this should work, check output and tell me the error if not

0

There's no error whatsoever. The value just either doesn't load or save.

0

That means the issue is not with saving. I assume it's because you are changing the value of kills LOCALLY, which makes it not detected by the save script that is server sided.

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