Hello roblox devs, my datastore script isn’t working:
Serverscript:
local DSS = game:GetService("DataStoreService")
local rs = game:GetService("ReplicatedStorage")
local database = DSS:GetDataStore("data")
local sessionData = {}
function playerAdded(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Rolls = Instance.new("IntValue")
Rolls.Name = "Rolls"
Rolls.Parent = leaderstats
local success = nil
local playerData = nil
local attempt = 1
repeat
success, playerData = pcall(function()
return database:GetAsync(player.UserId)
end)
attempt += 1
if not success then
warn(playerData)
task.wait(3)
end
until success or attempt == 5
if success then
print("Connected to database")
if not playerData then
print("Assigning default data")
playerData = {
["Rolls"] = 10
}
end
sessionData[player.UserId] = playerData
else
warn("Unable to get data for" .. player.UserId)
player:Kick("Unable to load your data. Try rejoining, if issue persists contact me on discord wallysaysdogs#9188")
end
Rolls.Value = sessionData[player.UserId].Rolls
end
function playerLeaving(player)
if sessionData[player.UserId] then
local success = nil
local errorMsg = nil
local attempt = 1
repeat
success, errorMsg = pcall(function()
database:SetAsync(player.UserId, sessionData[player.UserId])
end)
attempt += 1
if not success then
warn(errorMsg)
task.wait(3)
end
until success or attempt == 5
if success then
print("Data saved for, " .. player.Name)
print(sessionData)
else
warn("Unable to save data for " .. player.Name)
end
end
end
game.Players.PlayerAdded:Connect(playerAdded)
game.Players.PlayerRemoving:Connect(playerLeaving)
rs.Event.PlayerRolled.OnServerEvent:Connect(function(player)
wait(0.1)
print(game.Players:WaitForChild(player.Name):WaitForChild("leaderstats"):WaitForChild("Rolls").Value)
sessionData[player.UserId]["Rolls"] = game.Players:WaitForChild(player.Name):WaitForChild("leaderstats"):WaitForChild("Rolls").Value
print("Rolles changeded for ".. player.UserId .. " and session data is: ")
print(sessionData)
end)
My local script with the remote event:
local rollbutton = script.Parent.RollButton.Button
local rs=game:GetService("ReplicatedStorage")
local modules = rs.Modules
local auraData = require(modules.AuraData)
local resultUI = script.Parent.Parent.RollResultUI
local players = game:GetService("Players")
local localplayer = players.LocalPlayer
rollbutton.MouseButton1Click:Connect(function()
local roll = auraData.Roll(auraData.Auras)
localplayer:WaitForChild("leaderstats"):WaitForChild("Rolls").Value += 1
resultUI.Frame.Result.Text = tostring(roll[1])
resultUI.Frame.RNG.Text = tostring(roll[2])
resultUI.Frame.BackgroundColor3 = roll[3]
resultUI.Enabled = true
print(roll)
rs.Event.PlayerRolled:FireServer(localplayer)
end)
Any help is appreciated! I don’t get any errors, and the sessionData rolls value is always 0.