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

How to use DataStore with FilteringEnabled?

Asked by 6 years ago
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.

0
Can you specifically explain your problem? thesit123 509 — 6y
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. SchonATL 15 — 6y
0
Datastore only works in server scripts. cabbler 1942 — 6y
0
Thats what I'm using @cabbler SchonATL 15 — 6y
View all comments (2 more)
0
@SchonXIV, what exactly is/isn't working? Are there errors? chess123mate 5873 — 6y
0
@chess123mate There's no errors, and the value doesn't save. The folder and value shows up. SchonATL 15 — 6y

1 answer

Log in to vote
2
Answered by 6 years ago

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)
0
I appreciate the edit, but the problem is that FilteringEnabled breaks it. SchonATL 15 — 6y
0
This is a normal script under workspace or something, correct? There should be no issue as it is server sided. iamnoamesa 674 — 6y
0
I had it in ServerScriptService and it didn't work. Switched it to workspace and didn't work either. SchonATL 15 — 6y
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). chess123mate 5873 — 6y
View all comments (3 more)
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 iamnoamesa 674 — 6y
0
There's no error whatsoever. The value just either doesn't load or save. SchonATL 15 — 6y
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. iamnoamesa 674 — 6y
Ad

Answer this question