So I was trying to make a datastore script for my game, however, the datastores only work if filtering enabled is disabled, if it’s turned on it won’t save or load points and it will kick me from the game
Proof:
local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("KoolGamePig")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local points = Instance.new("IntValue")
points.Name = "Points"
points.Parent = leaderstats
local dosavedata = Instance.new("BoolValue")
dosavedata.Name = "DoSaveDataOnLeave"
dosavedata.Value = false
dosavedata.Parent = player
local success, err = pcall(function()
local loadeddata = myDataStore:GetAsync(player.UserId)
points.Value = loadeddata
print("Loaded value: "..loadeddata)
end)
if success then
print("Loaded data for "..player.Name.." successfully")
dosavedata.Value = true
else
print("FAILED TO LOAD DATA for "..player.Name)
player:Kick("Error loading data, kicked to prevent data loss")
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local dosavedata = player:FindFirstChild("DoSaveDataOnLeave")
if dosavedata and dosavedata.Value == true then
local success, err = pcall(function()
myDataStore:SetAsync(player.UserId, player.leaderstats.Points.Value)
print("Saved value: "..player.leaderstats.Points.Value)
end)
if success then
print("Saved data for "..player.Name.." successfully")
else
print("Failed to save data for "..player.Name.." with error "..err)
end
else
print("Told not to save for "..player.Name)
end
end)
I am a beginner at scripting so I don’t understand the problem.
Try to replace your code for that and see what is in output.
local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("KoolGamePig")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local points = Instance.new("IntValue")
points.Name = "Points"
points.Parent = leaderstats
local dosavedata = Instance.new("BoolValue")
dosavedata.Name = "DoSaveDataOnLeave"
dosavedata.Value = false
dosavedata.Parent = player
local success, err = pcall(function()
local loadeddata = myDataStore:GetAsync(player.UserId)
points.Value = loadeddata
print("Loaded value: "..loadeddata)
end)
if success then
print("Loaded data for "..player.Name.." successfully")
dosavedata.Value = true
else
print("FAILED TO LOAD DATA for "..player.Name)
warn(err)
player:Kick("Error loading data, kicked to prevent data loss")
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local dosavedata = player:FindFirstChild("DoSaveDataOnLeave")
if dosavedata and dosavedata.Value == true then
local success, err = pcall(function()
myDataStore:SetAsync(player.UserId, player.leaderstats.Points.Value)
print("Saved value: "..player.leaderstats.Points.Value)
end)
if success then
print("Saved data for "..player.Name.." successfully")
else
print("Failed to save data for "..player.Name.." with error "..err)
end
else
print("Told not to save for "..player.Name)
end
end)
You can’t disable Filtering Enabled anymore. Roblox disabled that feature since like 2016-2017 if I am not wrong. Anyway, try to do what Gaffal says and see if it works
local success, err = pcall(function()
local loadeddata = myDataStore:GetAsync(player.UserId)
if loadeddata then
print("Loaded value: "..tostring(loadeddata))
points.Value = loadeddata
end
end)
The problem was trying to concatenate a string with nil(the data)
At the first time you play the game, you have no data stored, so it was nil.
Entire code
local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("KoolGamePig")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local points = Instance.new("IntValue")
points.Name = "Points"
points.Parent = leaderstats
local dosavedata = Instance.new("BoolValue")
dosavedata.Name = "DoSaveDataOnLeave"
dosavedata.Value = false
dosavedata.Parent = player
local success, err = pcall(function()
local loadeddata = myDataStore:GetAsync(player.UserId)
if loadeddata then
print("Loaded value: "..tostring(loadeddata))
points.Value = loadeddata
dosavedata.Value = true
end
end)
if success then
print("Loaded data for "..player.Name.." successfully")
dosavedata.Value = true
else
print("FAILED TO LOAD DATA for "..player.Name)
warn(err)
player:Kick("Error loading data, kicked to prevent data loss")
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local dosavedata = player:FindFirstChild("DoSaveDataOnLeave")
if dosavedata and dosavedata.Value == true then
local success, err = pcall(function()
myDataStore:SetAsync(player.UserId, player.leaderstats.Points.Value)
print("Saved value: "..player.leaderstats.Points.Value)
end)
if success then
print("Saved data for "..player.Name.." successfully")
else
print("Failed to save data for "..player.Name.." with error "..err)
end
else
print("Told not to save for "..player.Name)
end
end)