Hello! I have been getting issues with datastores on my leaderboard!
The issue ive been getting is:
14:44:10.740 value of type nil cannot be converted to a number - Server - Leaderstats:31
14:44:10.740 Stack Begin - Studio
14:44:10.740 Script 'ServerScriptService.MainGameService.Leaderstats', Line 31 - Studio - Leaderstats:31
14:44:10.740 Script 'ServerScriptService.MainGameService.Leaderstats', Line 29 - Studio - Leaderstats:29
14:44:10.740 Stack End - Studio
And ive tried to fix it but it just doesnt work, Bassicly it detects if a player has already got data on his account and then it sets the data, and i keep getting errors because of it!
I cant fix it so heres the code
local plr
local char
local dss = game:GetService("DataStoreService")
local ds = dss:GetDataStore("TestMoneyValueNumber1")
game.Players.PlayerAdded:Connect(function(playeradded)
plr = playeradded
local LeaderstatsFolderInstance = Instance.new("Folder", playeradded)
LeaderstatsFolderInstance.Name = "leaderstats"
local MoneyValueInstance = Instance.new("NumberValue", LeaderstatsFolderInstance)
MoneyValueInstance.Name = "Money"
MoneyValueInstance:GetPropertyChangedSignal("Value"):Connect(function()
ds:SetAsync(playeradded.UserId, MoneyValueInstance.Value)
end)
if not ds then
ds:SetAsync(playeradded.UserId, 100)
end
local success, fail = pcall(function()
MoneyValueInstance.Value = ds:GetAsync(plr.UserId)
end)
if success then
print("Success")
elseif fail then
plr:Kick("Failed to load your data, Please jojn again :(")
end
plr.CharacterAdded:Connect(function(plrcharacteradded)
char = plrcharacteradded
end)
end)
game.Players.PlayerRemoving:Connect(function(playerremoved)
ds:SetAsync(playerremoved.UserId, playerremoved:FindFirstChild("leaderstats"):FindFirstChild("Money").Value)
playerremoved.CharacterRemoving:Connect(function(plrcharacterremoved)
end)
end)
anyone who tries to help: thanks because i cant do it myself