How can I fix the data in roblox - lua

I asked a second question because my last one was getting out of the subject with comments. So I'm trying to make roblox saving the stats of a player but it puts always the basic stats. Even if the player already played.
Here's my code :
local HttpService = game:GetService("HttpService")
local DataStoreService = game:GetService("DataStoreService")
local ds = DataStoreService:GetDataStore("Data")
game.Players.PlayerAdded:Connect(function(player)
local defaultData = {
coins = 0,
gems = 0,
xp = 0,
level = 1,
maxstamina = 100,
maxmagic = 100,
}
local loadedData = defaultData
local success, result = pcall(function()
return ds:GetAsync(player.UserId)
end)
if success then
if result then
-- if we've successfully loaded the data, parse the stored json data
print(string.format("An old player named %s has returned with data : %s!", player.Name, tostring(result)))
-- player data should look like this :
-- {"coins":0,"xp":0,"gems":0,"level":1,"maxstamina":100,"maxmagic":100}
local parseSuccess, parseResult = pcall(function()
return HttpService:JSONDecode(result)
end)
if parseSuccess then
loadedData = parseResult
else
warn(string.format("Failed to parse %s with error : %s", tostring(result), tostring(parseResult)))
end
else
-- we have a new player
print(string.format("New player named %s has joined!", player.Name))
end
else
warn(string.format("Something went wrong fetching %s's data : %s", player.Name, tostring(result)))
-- TO DO: FIGURE OUT HOW YOU WANT TO HANDLE THIS ERROR.
-- IF YOU DO NOTHING, THE PLAYER'S DATA WILL BE THE DEFAULT DATA USED FOR
-- NEW PLAYERS
end
-- create the leaderstats and hidden values, load the data from the loadedData table
local leaderstats = Instance.new("Model", player)
leaderstats.Name = "leaderstats"
local hidden = Instance.new("Model", player)
hidden.Name = "hidden"
local coins = Instance.new("IntValue", leaderstats)
coins.Name = "Coins"
coins.Value = loadedData.coins
local gems = Instance.new("IntValue", leaderstats)
gems.Name = "Gems"
gems.Value = loadedData.gems
local level = Instance.new("IntValue", leaderstats)
level.Name = "Level"
level.Value = loadedData.level
local xp = Instance.new("IntValue", hidden)
xp.Name = "XP"
xp.Value = loadedData.xp
local maxstamina = Instance.new("IntValue", hidden)
maxstamina.Name = "MaxStamina"
maxstamina.Value = loadedData.maxstamina
local maxmagic = Instance.new("IntValue", hidden)
maxmagic.Name = "MaxMagic"
maxmagic.Value = loadedData.maxmagic
local stamina = Instance.new("IntValue", hidden)
stamina.Name = "Stamina"
stamina.Value = loadedData.maxstamina
local magic = Instance.new("IntValue", hidden)
magic.Name = "Magic"
magic.Value = loadedData.maxmagic
end)
game.Players.PlayerRemoving:Connect(function(player)
local stats = player.leaderstats
local hidden = player.hidden
local data = {
coins = stats.Coins.Value,
gems = stats.Gems.Value,
xp = hidden.XP.Value,
level = stats.Level.Value,
maxstamina = hidden.MaxStamina.Value,
maxmagic = hidden.MaxMagic.Value,
}
-- wrap the request in a try-catch block to ensure that failures don't throw errors
local success, result = pcall(function()
-- save all the data as a JSON string
ds:setAsync(player.UserId, HttpService:JSONEncode(data))
end)
if not success then
warn(string.format("Failed to save %s's data with error : %s", player.Name, tostring(result)))
-- TO DO: FIGURE OUT HOW YOU WANT TO HANDLE THIS ERROR.
-- IF YOU DO NOTHING, THE PLAYER WILL LOSE THIS SESSION'S DATA
end
end)
More info :
The game is public but not finished here's the link if you wan't to test it : https://web.roblox.com/games/4867142155/Medieval-Fighting-Simulator-Beta?refPageId=b25ecb6b-40c9-4c28-a4bb-2e4e0a6c6d61
The Studio API Service is enabled
There's no error code
How can you help me?

I changed my answer :
So we must use DataStore2 and then it shall work!
Here's a good DataStore2 tutorial : https://www.youtube.com/watch?v=hBfMfB0BwGA
Here's my final code :
local DataStore2 = require(1936396537)
local defaultCoins = 0
local defaultGems = 0
local defaultLevel = 1
local defaultXP = 0
local defaultMStamina = 100
local defaultMMagic = 100
DataStore2.Combine("Data","coins","gems","level","xp","mstamina","mmagic")
local xpToLevelUp = function(level)
return 100 + level * 5
end
game.Players.PlayerAdded:Connect(function(player)
local coinsDataStore = DataStore2("coins",player)
local gemsDataStore = DataStore2("gems",player)
local levelDataStore = DataStore2("level",player)
local xpDataStore = DataStore2("xp",player)
local mstaminaDataStore = DataStore2("mstamina",player)
local mmagicDataStore = DataStore2("mmagic",player)
local stats = Instance.new("Folder",player)
stats.Name = "stats"
local coins = Instance.new("IntValue",stats)
coins.Name = "Coins"
local gems = Instance.new("IntValue",stats)
gems.Name = "Gems"
local level = Instance.new("IntValue",stats)
level.Name = "Level"
local xp = Instance.new("IntValue",stats)
xp.Name = "XP"
local mstamina = Instance.new("IntValue",stats)
mstamina.Name = "MStamina"
local stamina = Instance.new("IntValue",stats)
stamina.Name = "Stamina"
local mmagic = Instance.new("IntValue",stats)
mmagic.Name = "MMagic"
local magic = Instance.new("IntValue",stats)
magic.Name = "Magic"
local function coinsUpdate(updatedValue)
coins.Value = coinsDataStore:Get(updatedValue)
end
local function gemsUpdate(updatedValue)
gems.Value = gemsDataStore:Get(updatedValue)
end
local function levelUpdate(updatedValue)
level.Value = levelDataStore:Get(updatedValue)
end
local function xpUpdate(updatedValue)
if updatedValue >= xpToLevelUp(levelDataStore:Get(defaultLevel)) then
xpDataStore:Increment(xpToLevelUp(levelDataStore:Get(defaultLevel)) * -1)
levelDataStore:Increment(1)
else
player.stats.XP.Value = updatedValue
end
end
local function mStaminaUpdate(updatedValue)
mstamina.Value = mstaminaDataStore:Get(updatedValue)
end
local function mMagicUpdate(updatedValue)
mmagic.Value = mmagicDataStore:Get(updatedValue)
end
coinsUpdate(defaultCoins)
gemsUpdate(defaultGems)
levelUpdate(defaultLevel)
xpUpdate(defaultXP)
mStaminaUpdate(defaultMStamina)
mMagicUpdate(defaultMMagic)
stamina.Value = mstamina.Value
magic.Value = mmagic.Value
coinsDataStore:OnUpdate(coinsUpdate)
gemsDataStore:OnUpdate(gemsUpdate)
levelDataStore:OnUpdate(levelUpdate)
xpDataStore:OnUpdate(xpUpdate)
mstaminaDataStore:OnUpdate(mStaminaUpdate)
mmagicDataStore:OnUpdate(mMagicUpdate)
end)
game.ReplicatedStorage.Buy.OnServerEvent:Connect(function(player,cost,item)
local coinsDataStore = DataStore2("coins",player)
if player.leaderstats.Coins.Value < cost then
game.ReplicatedStorage.NotEnoughCoins:FireClient(player)
else
coinsDataStore:Increment(cost * -1,defaultCoins)
local buyedItem = game.ReplicatedStorage.Products:WaitForChild(item)
local clonedItem = buyedItem:Clone()
clonedItem.Parent = player.Backpack
end
end)
game.ReplicatedStorage.EarnCoins.OnServerEvent:Connect(function(player,coins)
local coinsDataStore = DataStore2("coins",player)
coinsDataStore:Increment(coins,defaultCoins)
end)
game.ReplicatedStorage.EarnGems.OnServerEvent:Connect(function(player,gems)
local gemsDataStore = DataStore2("gems",player)
gemsDataStore:Increment(gems,defaultGems)
end)
game.ReplicatedStorage.Enchance.OnServerEvent:Connect(function(player,cost,item,enchancedItem)
local gemsDataStore = DataStore2("gems",player)
if player.leaderstats.Gems.Value < cost then
game.ReplicatedStorage.NotEnoughGems:FireClient(player)
else
gemsDataStore:Increment(cost * -1,defaultGems)
item:Destroy()
local clonedItem = enchancedItem:Clone()
clonedItem.Parent = player.Backpack
end
end)
game.ReplicatedStorage.GainXP.OnServerEvent:Connect(function(player,amount)
local xpDataStore = DataStore2("xp",player)
xpDataStore:Increment(amount,defaultXP)
end)
game.ReplicatedStorage.LevelUp.OnServerEvent:Connect(function(player)
local mstaminaDataStore = DataStore2("mstamina",player)
local mmagicDataStore = DataStore2("mmagic",player)
mstaminaDataStore:Increment(25,defaultMStamina)
mmagicDataStore:Increment(25,defaultMMagic)
end)

Related

Why does all my values become zero before being able to save them in the onplayerRemoved

I tried everything i found but nothing seems to0 be working, here is my script
local DataStoreService = game:GetService('DataStoreService')
local playerData = DataStoreService:GetDataStore('PlayerData')
local datatobesaved
local function load(player: Player)
local leaderstats = Instance.new('Folder')
leaderstats.Name = 'leaderstats'
leaderstats.Parent = player
local LuckSauceIn = Instance.new('StringValue')
LuckSauceIn.Name = 'LuckSauce'
LuckSauceIn.Parent = leaderstats
LuckSauceIn.Value = "0"
local LuckSauce = Instance.new('NumberValue')
LuckSauce.Name = 'LuckSauceInt'
LuckSauce.Parent = player
local LuckUppgradeCost = Instance.new('NumberValue')
LuckUppgradeCost.Name = 'LuckUppgradeCost'
LuckUppgradeCost.Parent = player
LuckUppgradeCost.Value = 1000
local luckMultiplier = Instance.new('NumberValue')
luckMultiplier.Name = 'LuckMultiplier'
luckMultiplier.Parent = player
local autoClicksGamePass = Instance.new('BoolValue')
autoClicksGamePass.Name = 'autoClicksGamePassOwned'
autoClicksGamePass.Parent = player
autoClicksGamePass.Value = false
local instantClicksGamePass = Instance.new('BoolValue')
instantClicksGamePass.Name = 'instantClicksGamePassOwned'
instantClicksGamePass.Parent = player
instantClicksGamePass.Value = false
local doubleLuckGamePass = Instance.new('BoolValue')
doubleLuckGamePass.Name = 'doubleLuckGamePassOwned'
doubleLuckGamePass.Parent = player
doubleLuckGamePass.Value = false
local PlayerUserId = 'Player_'..player.UserId
local data = playerData:GetAsync(PlayerUserId)
if data then
print(data['LuckSauce'].." valuenow")
player.LuckSauceInt.Value = data['LuckSauce']
else
print("no data")
player.LuckSauceInt.Value = 0
end
end
local function save(player: Player)
print(player.LuckSauceInt.Value.." Money")
datatobesaved = {
LuckSauce = player.LuckSauceInt.Value
}
local playerid = 'Player_'..player.UserId
local success, err = pcall(function()
playerData:SetAsync(playerid, datatobesaved)
end)
if success then
print("data saved!")
else
print("data failed to save!")
end
end
game:BindToClose(function()
for i, plr in pairs(game.Players:GetPlayers()) do
save(plr)
end
end)
game.Players.PlayerAdded:Connect(function(player: Player)
load(player)
end)
game.Players.PlayerRemoving:Connect(function(player: Player)
print(player.leaderstats.LuckSauce.Value.." Money before")
save(player)
end)
i tried hard coding in wat the data should be by making it 5, so now it always loads as 5 so that works but the saving is the problem
Ive tried all the things people recomended on other posts but nothing works also i dont have it anywhere else that removes them either, i want the values to not become zero right before trying to save

I tried to make Loading Data screen but the data doesn't load correctly even when it's already saved the data

I was trying to make a Loading Data screen so it will fit perfectly and when the screen is fully loaded, fire a RemoteEvent that will load the data
I was expecting that this will work but instead of working, my data doesn't loaded correctly even when it's already saved on the previous game.
There are no error messages, as the codes are stable
I didn't tried any of the fixes, because I thought my code is fine enough.
Here's my code:
local function addData(player)
local Profile = Instance.new("Folder")
Profile.Name = "ProfileData"
Profile.Parent = player
local Books = Instance.new("Folder")
Books.Name = "BooksData"
Books.Parent = player
local Interactions = Instance.new("Folder")
Interactions.Name = "InteractionsActivity"
Interactions.Parent = player
local Settings = Instance.new("Folder")
Settings.Name = "SettingsData"
Settings.Parent = player
local Coins = Instance.new("IntValue")
Coins.Name = "Coins"
Coins.Parent = Profile
Coins.Value = 0
local Diamonds = Instance.new("IntValue")
Diamonds.Name = "Diamonds"
Diamonds.Parent = Profile
Diamonds.Value = 0
local XP = Instance.new("IntValue")
XP.Name = "XP"
XP.Parent = Profile
XP.Value = 0
local Level = Instance.new("IntValue")
Level.Name = "Level"
Level.Parent = Profile
Level.Value = 0
local IsSettingsOpen = Instance.new("IntValue")
IsSettingsOpen.Name = "IsSettingsOpen"
IsSettingsOpen.Parent = Interactions
local CurrentScreenOrientation = Instance.new("IntValue")
CurrentScreenOrientation.Name = "CurrentScreenOrientation"
CurrentScreenOrientation.Parent = Settings
end
local function loadData(player)
local PlayerId = "Player_" ..player.UserId
local SaveData = GameDataStore:GetAsync(PlayerId)
if SaveData then
player.ProfileData.Coins.Value = SaveData['Coins']
player.ProfileData.Diamonds.Value = SaveData['Diamonds']
player.ProfileData.XP.Value = SaveData['XP']
player.ProfileData.Level.Value = SaveData['Level']
player.InteractionsActivity.IsSettingsOpen.Value = SaveData['IsSettingsOpen']
player.SettingsData.CurrentScreenOrientation.Value = SaveData['CurrentScreenOrientation']
print("[GameData] Data loaded!")
else
player.ProfileData.Coins.Value = 0
player.ProfileData.Diamonds.Value = 0
player.ProfileData.XP.Value = 0
player.ProfileData.Level.Value = 0
player.InteractionsActivity.IsSettingsOpen.Value = 0
player.SettingsData.CurrentScreenOrientation.Value = 1
end
ReplicatedStorage.DataLoaded:FireClient(player)
print("[GameData] Firing DataLoaded RemoteEvent to Single Client")
end
local function saveData(player)
local SaveData = {
Coins = player.ProfileData.Coins.Value,
Diamonds = player.ProfileData.Diamonds.Value,
XP = player.ProfileData.XP.Value,
Level = player.ProfileData.Level.Value,
IsSettingsOpen = player.InteractionsActivity.IsSettingsOpen.Value,
CurrentScreenOrientation = player.SettingsData.CurrentScreenOrientation.Value
}
local PlayerId = "Player_" ..player.UserId
local success, err = pcall(function()
GameDataStore:SetAsync(PlayerId, SaveData)
end)
if success then
print("[GameData] Data has been sucessfully saved!")
else
warn("[Warning - GameData] Failed to save data! Check your internet connection and try again!")
end
end
Players.PlayerAdded:Connect(addData)
ReplicatedStorage.LoadData.OnServerEvent:Connect(loadData)
Players.PlayerRemoving:Connect(saveData)
And again, as my previous asked question one, I found out why this happens
In Roblox Studio, DataStore always fails and sometimes just work, but in Roblox, it's working like as expected.

Value doesn't save in dataStore

I need to save multies for my game. When i test it the value turns into 0.
Tried to make different dataStore only for multies but still doesn't work.
Console gives no errors. Only prints the value of Stat that doesn't change bc of multi.
DataTable also prints after closing the game that all multies are 0.
Code:
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local RunService = game:GetService("RunService")
local ServerStorage = game:GetService("ServerStorage")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local dataStore1 = DataStoreService:GetDataStore("Test1")
local function waitForRequestBudget(requestType)
local currentBudget = DataStoreService:GetRequestBudgetForRequestType(requestType)
while currentBudget < 1 do
currentBudget = DataStoreService:GetRequestBudgetForRequestType(requestType)
task.wait(5)
end
end
local function setupPlayerData(player: player)
local userID = player.UserId
local key = "Player_"..userID
local AgMulti = Instance.new("IntValue", player)
AgMulti.Name = "AgMulti"
AgMulti.Value = 1
local ReiatsuMulti = Instance.new("IntValue", player)
ReiatsuMulti.Name = "ReiatsuMulti"
ReiatsuMulti.Value = 1
local StrengthMulti = Instance.new("IntValue", player)
StrengthMulti.Name = "StrengthMulti"
StrengthMulti.Value = 1
local DuraMulti = Instance.new("IntValue", player)
DuraMulti.Name = "DuraMulti"
DuraMulti.Value = 1
local success, returnValue
repeat
waitForRequestBudget(Enum.DataStoreRequestType.GetAsync)
success, returnValue = pcall(dataStore1.GetAsync,dataStore1, key)
until success or not Players:FindFirstChild(player.Name)
success, returnValue = pcall(dataStore1.GetAsync,dataStore1, key)
if success then
if returnValue == nil then
returnValue = {
DuraMulti = 1,
StrengthMulti = 1,
ReiatsuMulti = 1,
AgMulti = 1,
}
end
StrengthMulti.Value = if returnValue.StrengthMulti ~= nil then returnValue.StrengthMulti else 0
DuraMulti.Value = if returnValue.DuraMulti ~= nil then returnValue.DuraMulti else 0
ReiatsuMulti.Value = if returnValue.ReiatsuMulti ~= nil then returnValue.ReiatsuMulti else 0
AgMulti.Value = if returnValue.AgMulti ~= nil then returnValue.AgMulti else 0
else
player:Kick("There was an error loading your Data. Roblox's DataStore might be down, try again later, or contact us!")
print(player.Name.."Data loading Error!")
end
end
local function save(player)
local userID = player.UserId
local key = "Player_"..userID
local strengthmulti = player.StrengthMulti.Value
local duramulti = player.DuraMulti.Value
local reiatsumulti = player.ReiatsuMulti.Value
local agmulti = player.AgMulti.Value
local dataTable = {
StrengthMulti = strengthmulti,
DuraMulti = duramulti,
ReiatsuMulti = reiatsumulti,
AgMulti = agmulti,
}
print(dataTable)
local success, returnValue
repeat
waitForRequestBudget(Enum.DataStoreRequestType.GetAsync)
success, returnValue = pcall(dataStore1.UpdateAsync, dataStore1, key, function()
return dataTable
end)
until success
if success then
print("Data Saved!")
else
print("Data saving Error!")
end
end
local function onShutDown()
if RunService:IsStudio() then
task.wait(2)
else
local finished = Instance.new("BindableEvent")
local allPlayers = Players:GetPlayers()
local leftPlayers = #allPlayers
for _, player in ipairs(allPlayers) do
coroutine.wrap(function()
save(player)
leftPlayers -=1
if leftPlayers == 0 then
finished:Fire()
end
end)
end
finished.Event:Wait()
end
end
for _, player in ipairs(Players:GetPlayers()) do
coroutine.wrap(setupPlayerData)(player)
end
Players.PlayerAdded:Connect(setupPlayerData)
Players.PlayerRemoving:Connect(save)
game:BindToClose(onShutDown)
while true do
task.wait(300)
for _, player in ipairs(Players:GetPlayers()) do
coroutine.wrap(save)(player)
end
end

Attempt to index nil with Position

I'm trying to do something like a spear throw and I'm so confused. It says:
ServerScriptService.FireMagic.FireSpear:16: attempt to index nil with 'Position'
Anyways, here's the LocalScript code:
wait(1)
local Player = game.Players.LocalPlayer
local Character = Player.Character
local Mouse = Player:GetMouse()
local rp = game:GetService("ReplicatedStorage")
local FireSpear = rp:WaitForChild("FireRemote")
local UIS = game:GetService("UserInputService")
local debounce = true
local cd = 10
UIS.InputBegan:Connect(function(input, isTyping)
if isTyping then
return
elseif input.KeyCode == Enum.KeyCode.E and debounce and Character then
debounce = false
FireSpear:FireServer()
wait(cd)
debounce = true
end
end)
and the Script:
wait(1)
local rp = game:GetService("ReplicatedStorage")
local ss = game:GetService("ServerStorage")
local Debris = game:GetService("Debris")
local ssFireSpear = ss.FireMagic:WaitForChild("ssFireSpear")
local FireRemote = rp:WaitForChild("FireRemote")
local UhTable = {}
local function LookatMouse(Mouse, RootPart)
local bodyG = Instance.new("BodyGyro")
bodyG.MaxTorque = Vector3.new(0, 500000, 0)
bodyG.P = 10000
bodyG.CFrame = CFrame.new(RootPart.Position, Mouse.Position)
bodyG.Parent = RootPart
Debris:AddItem(bodyG, 1)
end
local function MoveTowardsMouse(Mouse, Main)
local bodyV = Instance.new("BodyVelocity")
bodyV.MaxForce = Vector3.new(500000, 500000, 500000)
bodyV.Velocity = CFrame.new(Main.Position, Mouse.Position).LookVector * 100
bodyV.Parent = Main
local bodyG = Instance.new("BodyGyro")
bodyG.MaxTorque = Vector3.new(500000, 500000, 500000)
bodyG.P = 10000
bodyG.CFrame = CFrame.new(Main.Position, Mouse.Position)
bodyG.Parent = Main
end
FireRemote.OnServerEvent:Connect(function(Player, Mouse_CFrame)
if UhTable[Player.Name] == true then
return
end
UhTable[Player.Name] = true
local Character = Player.Character
local RootPart = Character:WaitForChild("HumanoidRootPart")
local folder = workspace:FindFirstChild("DebrisFolder") or Instance.new("Folder",workspace)
folder.Name = "DebrisFolder"
local RightHand = Character:WaitForChild("RightHand")
local FireSpear = ssFireSpear:Clone()
local Handle = FireSpear:WaitForChild("Handle")
local Hitbox = FireSpear:WaitForChild("Hitbox")
local Mesh = FireSpear:WaitForChild("Mesh")
FireSpear:SetPrimaryPartCFrame(RightHand.CFrame)
FireSpear.Parent = folder
local weld = Instance.new("Motor6D")
weld.Parent = Handle
weld.Part0 = RightHand
weld.Part1 = Handle
Hitbox:SetNetworkOwner(nil)
local function MakeStuffHappen()
spawn(function()
LookatMouse(Mouse_CFrame,RootPart)
wait(.6)
weld:Destroy()
MoveTowardsMouse(Mouse_CFrame,Hitbox)
end)
end
MakeStuffHappen()
end)
I'm following a tutorial but I don't know how the issue got there.
Your error is pointing to the fact that you are trying to reference fields on an object that doesn't exist. In this case, it's the ´Mouse´ object, which you never supplied from the client.
To fix this, pass the mouse information in when you call the RemoteEvent's FireServer() function.
UIS.InputBegan:Connect(function(input, isTyping)
if isTyping then
return
elseif input.KeyCode == Enum.KeyCode.E and debounce and Character then
debounce = false
local mouseCFrame = Mouse.Hit
FireSpear:FireServer(mouseCFrame)
wait(cd)
debounce = true
end
end)

Data problems in roblox

I'm trying to save data in roblox unfortunatly. It don't works can you help me?
Here's my code :
local ds = game:GetService("DataStoreService"):GetDataStore("Data")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Model")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local hidden = Instance.new("Model")
hidden.Name = "hidden"
hidden.Parent = player
local coins = Instance.new("IntValue")
coins.Name = "Coins"
coins.Parent = leaderstats
coins.Value = ds:GetAsync(player.UserId.."-coins") or 0
ds:SetAsync(player.UserId.."-coins", coins.Value)
coins.Changed:Connect(function()
ds:SetAsync(player.UserId.."-coins", coins.Value)
end)
local gems = Instance.new("IntValue")
gems.Name = "Gems"
gems.Parent = leaderstats
gems.Value = ds:GetAsync(player.UserId.."-gems") or 0
ds:SetAsync(player.UserId.."-gems", gems.Value)
gems.Changed:Connect(function()
ds:SetAsync(player.UserId.."-gems", gems.Value)
end)
local level = Instance.new("IntValue")
level.Name = "Level"
level.Parent = leaderstats
level.Value = ds:GetAsync(player.UserId.."-level") or 1
ds:SetAsync(player.UserId.."-level", level.Value)
level.Changed:Connect(function()
ds:SetAsync(player.UserId.."-level", level.Value)
end)
local xp = Instance.new("IntValue")
xp.Name = "XP"
xp.Parent = hidden
xp.Value = ds:GetAsync(player.UserId.."-xp") or 0
ds:SetAsync(player.UserId.."-xp", xp.Value)
xp.Changed:Connect(function()
ds:SetAsync(player.UserId.."-xp", xp.Value)
end)
local maxstamina = Instance.new("IntValue")
maxstamina.Name = "MaxStamina"
maxstamina.Parent = hidden
maxstamina.Value = ds:GetAsync(player.UserId.."-maxstamina") or 100
ds:SetAsync(player.UserId.."-maxstamina", maxstamina.Value)
maxstamina.Changed:Connect(function()
ds:SetAsync(player.UserId.."-maxstamina", maxstamina.Value)
end)
local maxmagic = Instance.new("IntValue")
maxmagic.Name = "MaxMagic"
maxmagic.Parent = hidden
maxmagic.Value = ds:GetAsync(player.UserId.."-maxmagic") or 100
ds:SetAsync(player.UserId.."-maxmagic", maxmagic.Value)
maxmagic.Changed:Connect(function()
ds:SetAsync(player.UserId.."-maxmagic", maxmagic.Value)
end)
local stamina = Instance.new("IntValue")
stamina.Name = "Stamina"
stamina.Parent = hidden
stamina.Value = maxstamina.value
local magic = Instance.new("IntValue")
magic.Name = "Magic"
magic.Parent = hidden
magic.Value = maxmagic.value
end)
game.Players.PlayerRemoving:Connect(function(player)
ds:setAsync(player.UserId.."-coins", player.leaderstats.Coins.Value)
ds:setAsync(player.UserId.."-gems", player.leaderstats.Gems.Value)
ds:setAsync(player.UserId.."-xp", player.hidden.XP.Value)
ds:setAsync(player.UserId.."-level", player.leaderstats.Level.Value)
ds:setAsync(player.UserId.."-maxstamina", player.hidden.MaxStamina.Value)
ds:setAsync(player.UserId.."-maxmagic", player.hidden.MaxMagic.Value)
end)
Thank's for helping me!
Also there's no error code it's just than when i test it my data don't save.
I have enabled the Studio Access to API Services.
Really i don't know what's happening i passed hours searching how to solve this but i didn't found it.
It could be that your game hasn't been published yet, but nothing in your code looks like it would throw syntax errors, but it does look unsafe and wasteful.
Every call to GetAsync() and SetAsync() is a blocking network request that has a chance to fail and throw errors. Plus, there is a limit on the number of requests that you can send from the server, and if you hit that limit, your code will throw errors and will likely lose player data.
Rather than save one value per key, you can save a whole table of values into a single key. This greatly reduces the number of network requests and the chances that something could fail.
Saving Data
local HttpService = game:GetService("HttpService")
local DataStoreService = game:GetService("DataStoreService")
local ds = DataStoreService:GetDataStore("Data")
game.Players.PlayerRemoving:Connect(function(player)
local stats = player.leaderstats
local hidden = player.hidden
local data = {
coins = stats.Coins.Value,
gems = stats.Gems.Value,
xp = hidden.XP.Value,
level = stats.Level.Value,
maxstamina = hidden.MaxStamina.Value,
maxmagic = hidden.MaxMagic.Value,
}
-- wrap the request in a try-catch block to ensure that failures don't throw errors
local success, result = pcall(function()
-- save all the data as a JSON string
ds:setAsync(player.UserId, HttpSevice:JSONEncode(data))
end
if not success then
warn(string.format("Failed to save %s's data with error : %s", player.Name, tostring(result))
-- TO DO: FIGURE OUT HOW YOU WANT TO HANDLE THIS ERROR.
-- IF YOU DO NOTHING, THE PLAYER WILL LOSE THIS SESSION'S DATA
end
end)
Loading Data
game.Players.PlayerAdded:Connect(function(player)
local defaultData = {
coins = 0,
gems = 0,
xp = 0,
level = 1,
maxstamina = 100,
maxmagic = 100,
}
local loadedData = defaultData
local success, result = pcall(function()
return ds:GetAsync(player.UserId)
end
if success then
if result then
-- if we've successfully loaded the data, parse the stored json data
print(string.format("An old player named %s has returned with data : %s!", player.Name, result))
-- player data should look like this :
-- {"coins":0,"xp":0,"gems":0,"level":1,"maxstamina":100,"maxmagic":100}
local parseSuccess, parseResult = pcall(function()
return HttpService:JSONDecode(result)
end)
if parseSuccess then
loadedData = parseResult
else
warn(string.format("Failed to parse %s with error : %s", tostring(result), tostring(parseResult))
end
else
-- we have a new player
print(string.format("New player named %s has joined!", player.Name))
end
else
warn(string.format("Something went wrong fetching %s's data : %s", player.Name, tostring(result))
-- TO DO: FIGURE OUT HOW YOU WANT TO HANDLE THIS ERROR.
-- IF YOU DO NOTHING, THE PLAYER'S DATA WILL BE THE DEFAULT DATA USED FOR
-- NEW PLAYERS
end
-- create the leaderstats and hidden values, load the data from the loadedData table
local leaderstats = Instance.new("Model")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local hidden = Instance.new("Model")
hidden.Name = "hidden"
hidden.Parent = player
local coins = Instance.new("IntValue")
coins.Name = "Coins"
coins.Parent = leaderstats
coins.Value = loadedData.coins
local gems = Instance.new("IntValue")
gems.Name = "Gems"
gems.Parent = leaderstats
gems.Value = loadedData.gems
local level = Instance.new("IntValue")
level.Name = "Level"
level.Parent = leaderstats
level.Value = loadedData.level
local xp = Instance.new("IntValue")
xp.Name = "XP"
xp.Parent = hidden
xp.Value = loadedData.xp
local maxstamina = Instance.new("IntValue")
maxstamina.Name = "MaxStamina"
maxstamina.Parent = hidden
maxstamina.Value = loadedData.maxstamina
local maxmagic = Instance.new("IntValue")
maxmagic.Name = "MaxMagic"
maxmagic.Parent = hidden
maxmagic.Value = loadedData.maxmagic
local stamina = Instance.new("IntValue")
stamina.Name = "Stamina"
stamina.Parent = hidden
stamina.Value = loadedData.maxstamina
local magic = Instance.new("IntValue")
magic.Name = "Magic"
magic.Parent = hidden
magic.Value = loadedData.maxmagic
end)

Resources