Hi there! I’m making a simulator game but I have a problem. My multiplier is acting strangely. Here is the data store script I applied:
local dataStore = game:GetService("DataStoreService"):GetDataStore("PlayerData")
local function onServerShutdown()
for _, player in pairs(game.Players:GetPlayers()) do
local success, errmsg = pcall(function()
dataStore:SetAsync(player.UserId, {player.leaderstats.Protection.Value, player.leaderstats.Rebirths.Value, player.Multiplier.Value})
end)
if success then
print("Successfully saved your data!")
else
print("Unable to save data: "..errmsg)
warn(errmsg)
end
end
end
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local protect = Instance.new("IntValue")
protect.Name = "Protection"
protect.Parent = leaderstats
protect.Value = 0
local rebirths = Instance.new("IntValue")
rebirths.Name = "Rebirths"
rebirths.Parent = leaderstats
rebirths.Value = 0
local multiplier = Instance.new("NumberValue")
multiplier.Name = "Multiplier"
multiplier.Parent = player
multiplier.Value = 1
rebirths.Changed:Connect(function()
if rebirths.Value + 1 then
multiplier.Value += 1
end
end)
local data
local success, errmsg = pcall(function()
data = dataStore:GetAsync(player.UserId)
end)
if not success then
player:Kick("Fetch Data Error: "..errmsg..". Therefore we had to kick you to prevent data loss. Sorry! :(")
warn(errmsg)
end
if success then
if data then
protect.Value = data[1]
rebirths.Value = data[2]
multiplier.Value = data[3]
print("Welcome back "..player.Name.."! You had "..protect.Value.." protection since last play.")
else
print("This is a new player or they just have no data.")
end
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, errmsg = pcall(function()
dataStore:SetAsync(player.UserId, {player.leaderstats.Protection.Value, player.leaderstats.Rebirths.Value, player.Multiplier.Value})
end)
if success then
print("Successfully saved your data!")
else
print("Unable to save data: "..errmsg)
warn(errmsg)
end
end)
game:BindToClose(onServerShutdown)
The multiplier is supposed to start at 1 but it keeps setting at 0 (Even when I switch it to leaderstats). Is there a way to fix this issue? All suggestions are appreciated!
I might has thought of the problem, the value of it may allready be 0 in the datastore so it sets it to that. You could change the data store you are using to test that or check it with the legendary DataStore Editor plugin.
I actually had a module script for it before I written this one. Sadly, it didn’t work either.
local DSS = game:GetService("DataStoreService")
local dataStore = DSS:GetDataStore("PlayerData")
local dataStoreSettings = {}
function dataStoreSettings:GetKeyData(key)
return dataStore:GetAsync(key)
end
function dataStoreSettings:SetDataToTable(key, tableToSave)
dataStore:SetAsync(key, tableToSave)
end
return dataStoreSettings
local dataStoreSettings = require(script.DataStoreModules.MainDataFunctions)
local function onServerShutdown()
for _, player in pairs(game.Players:GetPlayers()) do
local success, errmsg = pcall(function()
dataStoreSettings:SetDataToTable(player.UserId, {player.leaderstats.Protection.Value, player.leaderstats.Rebirths.Value, player.Multiplier.Value})
end)
if success then
print("Successfully saved your data!")
else
print("Unable to save data: "..errmsg)
warn(errmsg)
end
end
end
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local protect = Instance.new("IntValue")
protect.Name = "Protection"
protect.Parent = leaderstats
protect.Value = 0
local rebirths = Instance.new("IntValue")
rebirths.Name = "Rebirths"
rebirths.Parent = leaderstats
rebirths.Value = 0
local multiplier = Instance.new("NumberValue")
multiplier.Name = "Multiplier"
multiplier.Parent = player
multiplier.Value = 1
rebirths.Changed:Connect(function()
if rebirths.Value + 1 then
multiplier.Value += 1
end
end)
local data
local success, errmsg = pcall(function()
data = dataStoreSettings:GetKeyData(player.UserId)
end)
if not success then
player:Kick("Fetch Data Error: "..errmsg..". Therefore we had to kick you to prevent data loss. Sorry! :(")
warn(errmsg)
end
if success then
if data then
protect.Value = data[1]
rebirths.Value = data[2]
multiplier.Value = data[3]
print("Welcome back "..player.Name.."! You had "..protect.Value.." protection since last play.")
else
print("This is a new player or they just have no data.")
end
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, errmsg = pcall(function()
dataStoreSettings:SetDataToTable(player.UserId, {player.leaderstats.Protection.Value, player.leaderstats.Rebirths.Value, player.Multiplier.Value})
end)
if success then
print("Successfully saved your data!")
else
print("Unable to save data: "..errmsg)
warn(errmsg)
end
end)
game:BindToClose(onServerShutdown)
By the way, I find it strange because the rebirth and other value works perfectly.