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
Related
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'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)
For a bit of context, I'm making a Tycoon on Roblox to test my knowledge in lua. Everything was working fine until I tried making leaderstats. The first script, which is what makes the Tycoon
function, is here:
local TycoonInfo = script.Parent:WaitForChild("TycoonInfo")
local OwnerValue = TycoonInfo:WaitForChild("Owner")
local Buttons = script.Parent:WaitForChild("Buttons")
local Purchases = script.Parent:WaitForChild("Purchases")
local Objects = {}
function validateHitByOwner(Hit)
if Hit and Hit.Parent and Hit.Parent:FindFirstChild("Humanoid") then
local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)
if Player and OwnerValue.Value== Player then
return true
end
end
return false
end
for i,v in pairs(Buttons:GetChildren()) do
local Object = Purchases:FindFirstChild(v.Object.Value)
if Object then
Objects[Object.Name] = Object:Clone()
Object:Destroy()
if v:FindFirstChild("Dependency") then
coroutine.resume(coroutine.create(function()
v.Button.Transparency = 1
v.Button.BillboardGui.Enabled = false
v.Button.CanCollide = false
if Purchases:WaitForChild(v.Dependency.Value,90000) then
v.Button.Transparency = 0
v.Button.CanCollide = true
v.Button.BillboardGui.Enabled = true
end
end))
end
local DB = false
v.Button.Touched:Connect(function(Hit)
if validateHitByOwner(Hit)then
if DB == false then
DB = true
if v.Button.Transparency == 0 then
local Purchased = DoPurchase(Hit,v,v.Price.Value,Objects[v.Object.Value])
if Purchased then
warn("Purchased")
else
warn("Can't purchase")
end
end
DB = false
end
end
end)
end
end
function DoPurchase(Hit,Button,Cost,Object)
if Hit and Hit.Parent and Hit.Parent:FindFirstChild("Humanoid") then
local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)
if Player and OwnerValue.Value == Player then
local name = game.Players:GetChildren()
local CashValue = game.Players.name.leaderstats.Cash -- Error here
if CashValue then
if CashValue.Value >= Cost then
CashValue.Value = CashValue.Value - Cost
Object.Parent = Purchases
Button:Destroy()
return true
end
end
end
end
return false
end
The next script, which makes the leaderboard and assigns a player that has recently joined to a random available Tycoon, is here:
function FindEmptyTycoon()
for i,v in pairs(workspace.Tycoons:GetChildren()) do
if v.TycoonInfo.Owner.Value == nil then
return v
end
end
return nil
end
game.Players.PlayerAdded:Connect(function(Player)
local Tycoon = FindEmptyTycoon()
Tycoon.TycoonInfo.Owner.Value = Player
local stats = Instance.new("Folder",Player)
stats.Name = "leaderstats"
local Cash = Instance.new("IntValue",stats)
Cash.Name = "Cash"
end)
And then, the script that is stored in all purchasable items to give the Player money, is here:
function giveCash(player)
wait(3.33)
local Cash = player.leaderstats.Cash
Cash.Value = Cash.Value + 2
end
By the way, these scripts are versions of the original scripts that I've been trying to edit so they can actually work, and I think I've been getting close to fixing the errors. The original scripts can be found here (in order of appearance):
local TycoonInfo = script.Parent:WaitForChild("TycoonInfo")
local OwnerValue = TycoonInfo:WaitForChild("Owner")
local Buttons = script.Parent:WaitForChild("Buttons")
local Purchases = script.Parent:WaitForChild("Purchases")
local Objects = {}
function validateHitByOwner(Hit)
if Hit and Hit.Parent and Hit.Parent:FindFirstChild("Humanoid") then
local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)
if Player and OwnerValue.Value== Player then
return true
end
end
return false
end
for i,v in pairs(Buttons:GetChildren()) do
local Object = Purchases:FindFirstChild(v.Object.Value)
if Object then
Objects[Object.Name] = Object:Clone()
Object:Destroy()
if v:FindFirstChild("Dependency") then
coroutine.resume(coroutine.create(function()
v.Button.Transparency = 1
v.Button.BillboardGui.Enabled = false
v.Button.CanCollide = false
if Purchases:WaitForChild(v.Dependency.Value,90000) then
v.Button.Transparency = 0
v.Button.CanCollide = true
v.Button.BillboardGui.Enabled = true
end
end))
end
local DB = false
v.Button.Touched:Connect(function(Hit)
if validateHitByOwner(Hit)then
if DB == false then
DB = true
if v.Button.Transparency == 0 then
local Purchased = DoPurchase(Hit,v,v.Price.Value,Objects[v.Object.Value])
if Purchased then
warn("Purchased")
else
warn("Can't purchase")
end
end
DB = false
end
end
end)
end
end
function DoPurchase(Hit,Button,Cost,Object)
if Hit and Hit.Parent and Hit.Parent:FindFirstChild("Humanoid") then
local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)
if Player and OwnerValue.Value == Player then
local CashValue = game.ServerStorage.PlayerCash:FindFirstChild(Player.Name)
if CashValue then
if CashValue.Value >= Cost then
CashValue.Value = CashValue.Value - Cost
Object.Parent = Purchases
Button:Destroy()
return true
end
end
end
end
return false
end
function FindEmptyTycoon()
for i,v in pairs(workspace.Tycoons:GetChildren()) do
if v.TycoonInfo.Owner.Value == nil then
return v
end
end
return nil
end
game.Players.PlayerAdded:Connect(function(Player)
local Tycoon = FindEmptyTycoon()
Tycoon.TycoonInfo.Owner.Value = Player
local stats = Instance.new("BoolValue",Player)
stats.Name = "leaderstats"
stats.Parent = game.ServerStorage
local Cash = Instance.new("IntValue",stats)
Cash.Name = Player.Name
Cash.Value = 0
Cash.Parent = game.ServerStorage.PlayerCash
end)
local amount = 2
local timedelay = 2.33
local currencyname = "Cash"
wait(1)
while true do
wait(timedelay)
for i,v in pairs(game.ServerStorage.PlayerCash:GetChildren()) do
if v:FindFirstChild("leaderstats") and v then
v.leaderstats[currencyname].Value = v.leaderstats[currencyname].Value + amount
end
end
end
local name = game.Players:GetChildren()
Why do you store the list of Players's children in a variable called name? This function returns a table. name would be more suitable for a string.
local CashValue = game.Players.name.leaderstats.Cash -- Error here
game.Players is https://developer.roblox.com/en-us/api-reference/class/Players
I cannot make sense of game.Players.name and even less of game.Players.name.leaderstats
Did you mean Player.leaderstats?
I would expect an error message for Players.name.leaderstats before facing an error for game.Players.name.leaderstats.Cash
name is not a Property of Players so you should not be allowed to index Players.name
I'm no Roblox expert but from what I see your code is so off that you really should start with simple tutorials rather than with fixing this code.
I think you should start over and make sure you understand how Instance.new is used properly and such things.
Using better variable names will also help.
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)
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)