Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

why does mny deaths leaderboard not appear?

nothing seems wrong but my deaths leaderboard does not appear line 9(i added leaderstats folder in a different script, everything else is data store)

local dss = game:GetService("DataStoreService")

local deathsDataStore = dss:GetDataStore("deathsDataStore")

game.Players.PlayerAdded:Connect(function(player)
	
	local leaderstats = player:WaitForChild("leaderstats")
	
	local deaths = Instance.new("IntValue")
	deaths.Name = "Deaths"
	deaths.Parent = leaderstats
	
	local data
	local success, errormsg = pcall(function()
		data = deathsDataStore:GetAsync(player.UserId.."-deaths")
	end)
	
	if data ~= nil then
		deaths.Value = data
		print(player.Name.."'s deaths data was retrieved!")
	else
		print("Error while getting "..player.Name.."'s deaths data.")
		warn(errormsg)
	end
	
end)

game.Players.PlayerRemoving:Connect(function(player)
	
	local success, errormsg = pcall(function()
		deathsDataStore:SetAsync(player.UserId.."deaths",player.leaderstats.Deaths.Value)
	end)
	
	if success then
		print(player.Name.."'s deaths data was saved!")
	else
		print("Error when saving "..player.Name.."'s deaths data.")
		warn(errormsg)
	end
	
end)

1 answer

✓ Accepted answer

Try making the data show up if it is nil, cuz right now it only shows up if it isn't nil.

local dss = game:GetService("DataStoreService")

local deathsDataStore = dss:GetDataStore("deathsDataStore")

game.Players.PlayerAdded:Connect(function(player)

    local leaderstats = player:WaitForChild("leaderstats")

    local deaths = Instance.new("IntValue")
    deaths.Name = "Deaths"
    deaths.Parent = leaderstats

    local data
    local success, errormsg = pcall(function()
        data = deathsDataStore:GetAsync(player.UserId.."-deaths")
    end)

    if data == nil then
          data = 0
    end

    if data ~= nil then
        deaths.Value = data
        print(player.Name.."'s deaths data was retrieved!")
    else
        print("Error while getting "..player.Name.."'s deaths data.")
        warn(errormsg)
    end

end)

game.Players.PlayerRemoving:Connect(function(player)

    local success, errormsg = pcall(function()
        deathsDataStore:SetAsync(player.UserId.."deaths",player.leaderstats.Deaths.Value)
    end)

    if success then
        print(player.Name.."'s deaths data was saved!")
    else
        print("Error when saving "..player.Name.."'s deaths data.")
        warn(errormsg)
    end

end)

Data being nil means the player has no record of their Deaths in the datastore.

This discussion is preserved as it was. New answers, comments, and votes are closed.