I tried to save leaderstats with multiple variants of code and even asked some friends to help, but nothing seems to work, api is turned on and everything should work beacuse i dont even get an error, but after i rejoining the leaderstats always shows the value of money and income as 0
#Cant save leaderstats
89 messages · Page 1 of 1 (latest)
You have to do it this way:
local success, errorMsg = pcall(function()
Save1:SetAsync(Player.UserId, Player.leaderstats.Money.Value)
Save2:SetAsync(Player.UserId, Player.leaderstats.Income.Value)
end)
if not success then
warn(errorMsg)
end
ServerScriptService.leaderstats:21: attempt to index nil with 'UserId'
.
Did you remove game.Players.PlayerRemoving
.
Line 20
yeah
Don't
Repalce line 21 and 22 with this
local success, errorMsg = pcall(function()
Save1:SetAsync(Player.UserId, Player.leaderstats.Money.Value)
Save2:SetAsync(Player.UserId, Player.leaderstats.Income.Value)
end)
if not success then
warn(errorMsg)
end
end)
like this?
if yes that dosent seem to work
is there another way?
** You are now Level 1! **
Let me check rq.
alr
Everything works for me bro.
-- script.Parent = game.ServerScriptService
local DS = game:GetService("DataStoreService")
local save1 = DS:GetDataStore("Money")
game.Players.PlayerAdded:Connect(function(plr)
local leadertats = Instance.new("Folder", plr)
leadertats.Name = "leaderstats"
local money = Instance.new("IntValue", leadertats)
money.Name = "Money"
money.Value = save1:GetAsync(plr.UserId) or 0
end)
game.Players.PlayerRemoving:Connect(function(plr)
local success, errorMsg = pcall(function()
save1:SetAsync(plr.UserId, plr.leaderstats.Money.Value)
end)
if not success then
warn(errorMsg)
end
end)
strange very strange
Okay I'm back.
I had to eat dinner.
Anyway it should've been done like this:
local DS = game:GetService("DataStoreService")
local dsStore = DS:GetDataStore("PlayerStats")
game.Players.PlayerAdded:Connect(function(plr)
local leadertats = Instance.new("Folder", plr)
leadertats.Name = "leaderstats"
local money = Instance.new("IntValue", leadertats)
money.Name = "Money"
local income = Instance.new("IntValue", leadertats)
income.Name = "Income"
local data
local success, errorMsg = pcall(function()
data = dsStore:GetAsync("midata_"..plr.UserId)
end)
if not success then
warn(errorMsg)
end
money.Value = data[1]
income.Value = data[2]
end)
game.Players.PlayerRemoving:Connect(function(plr)
local money = plr.leaderstats.Money
local income = plr.leaderstats.Income
local data = {money.Value, income.Value}
local success, errorMsg = pcall(function()
dsStore:SetAsync("midata_"..plr.UserId, data)
end)
if not success then
warn(errorMsg)
end
end)
still dosent seem to work
Are you changing the values here on the client or on the server
server
Why
idk scripting gods seems to hate me
but first time i put the script i had an error
ServerScriptService.leaderstats:24: attempt to index nil with number
but at the second launch it dissapeard
Oh right, we forgot to check if the data even exists.
local DS = game:GetService("DataStoreService")
local dsStore = DS:GetDataStore("PlayerStats")
game.Players.PlayerAdded:Connect(function(plr)
local leadertats = Instance.new("Folder", plr)
leadertats.Name = "leaderstats"
local money = Instance.new("IntValue", leadertats)
money.Name = "Money"
local income = Instance.new("IntValue", leadertats)
income.Name = "Income"
local data
local success, errorMsg = pcall(function()
data = dsStore:GetAsync("midata_"..plr.UserId)
end)
if not success then
warn(errorMsg)
end
if data then
money.Value = data[1]
income.Value = data[2]
else
money.Value = 0
income.Value = 0
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
local money = plr.leaderstats.Money
local income = plr.leaderstats.Income
local data = {money.Value, income.Value}
local success, errorMsg = pcall(function()
dsStore:SetAsync("midata_"..plr.UserId, data)
end)
if not success then
warn(errorMsg)
end
end)
Check
i think its another script that dosent let the save data to be saved but idk what script
does the script give any error now?
Then check them all.
no it dosent but the data never gets saved
If this is the case then there are only 3 options why it doesnt work
- roblox servers have broke (not this case)
- You havent enabled API services (if not then go to settings or search a yt vid)
- You are changing the Money and Income value through the client side
This is probably what you re doing
When you open to test the game
you need to press Client on top right
local Money = Player:WaitForChild("leaderstats"):WaitForChild("Money")
local MarketPlaceService = game:GetService("MarketplaceService")
local GamepassID = 170651464
local button = script.Parent
local obuchenie = true
local function clicked()
if obuchenie == true then
button.Parent:FindFirstChild("Tutorial"):Destroy()
obuchenie = false
end
local sound = button:FindFirstChild("meow"):Clone()
sound.Parent = workspace.Trash
sound:Play()
task.wait(1.35)
sound:Destroy()
end
script.Parent.MouseButton1Click:Connect(function()
if MarketPlaceService:UserOwnsGamePassAsync(Player.UserId, GamepassID) then
Player.leaderstats.Money.Value = Player.leaderstats.Money.Value + 2
else
Player.leaderstats.Money.Value = Player.leaderstats.Money.Value + 1
end
end)
script.Parent.MouseButton1Click:Connect(function()
game.Workspace.Sound1:Play()
end)```
script for the click
Try to change it without those first
alr
Is it on the client?
Open the test, switch to server side and change value from there first
Yes, tahts probably why it doesnt save
probably
** You are now Level 2! **
Is the script icon blue or white?
Because he stores values in the client but never replicates them to the server which cant save any data
its local
He said he tested it on the server.
First line is:
local player = game.Players.LocalPlayer
Can't read it right now.
Then change it to server.
He cant because its an UI
he would have to use a remote event
i hate events
Uh-huh.
Sadly they re one of the only way to communicate between client and server
They're fairly simple.
I read the script, dont worry
ill go learn them then learn how i can change from client to server
ill give an update if it works or dosent
Sure.
Scripting a UI through server isnt reccomended though
isnt there a way to save the values that are changed from client?
Server handles data, not the client
Because data is sent to roblox servers which store the data