Error leaderstats is not a valid member of Player Players.paulocezarnbr

Hello i created a speed simulator but some servers have a issue and players get default speed how i can fix this
Speed Script (Server) :


while wait(.5) do
	local children = game.Players:GetChildren()
	for i = 1 , #children do
		if children [i].Character ~= nil then
			local hum = children [i].Character.Humanoid
			hum.WalkSpeed = children [i].leaderstats.Points.Value / 3.12 + 1
		end
	end
end

Leaderstats Script (Server):

PointData = game:GetService("DataStoreService"):GetDataStore("PointData")
PrestigeData = game:GetService("DataStoreService"):GetDataStore("PrestigeData")

game.Players.PlayerAdded:Connect(function(plr)
	local LB = Instance.new("IntValue")
	LB.Name = "leaderstats"

	local p = Instance.new("IntValue")
	p.Name = "Points"
	p.Value = PointData:GetAsync(plr.userId) or 0

	local r = Instance.new("IntValue")
	r.Name = "Prestige"
	r.Value = PrestigeData:GetAsync(plr.userId) or 0

	LB.Parent = plr
	p.Parent = LB
	r.Parent = LB
end)

game.Players.PlayerRemoving:Connect(function(plr)
	PointData:SetAsync(plr.userId, plr.leaderstats.Points.Value)
	PrestigeData:SetAsync(plr.userId, plr.leaderstats.Prestige.Value)
end)

This should be a folder, not an IntValue:

	local LB = Instance.new("Folder")
	LB.Name = "leaderstats"

Race condition where your while loop is running before leaderstats gets created. You shouldn’t assume things exist when you try to index them, especially if they’re dynamically created and not present right away when the server starts up.

2 Likes

Please put down a WaitForChild() before it checks for leaderstats, it assumes it’s there when it clearly isn’t. something like “children[i]:WaitForChild(“leaderstats”)” should suffice.

1 Like
local players = game:GetService("Players")

while task.wait(0.5) do
	for _, player in ipairs(players:GetPlayers()) do
		local leaderstats = player:FindFirstChild("leaderstats")
		if leaderstats then
			local points = leaderstats:FindFirstChild("Points")
			if points then
				local character = player.Character
				local humanoid = character:FindFirstChild("Humanoid")
				humanoid.WalkSpeed = points.Value / 3.14
			end
		end
	end
end
local DSS = game:GetService("DataStoreService")
local PointData = DSS:GetDataStore("PointData")
local PrestigeData = DSS:GetDataStore("PrestigeData")
local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(plr)
	local LB = Instance.new("Folder")
	LB.Name = "leaderstats"
	LB.Parent = plr

	local p = Instance.new("IntValue")
	p.Name = "Points"
	p.Value = PointData:GetAsync(plr.userId) or 0
	p.Parent = LB

	local r = Instance.new("IntValue")
	r.Name = "Prestige"
	r.Value = PrestigeData:GetAsync(plr.userId) or 0
	r.Parent = LB
end)

Players.PlayerRemoving:Connect(function(plr)
	PointData:SetAsync(plr.userId, plr.leaderstats.Points.Value)
	PrestigeData:SetAsync(plr.userId, plr.leaderstats.Prestige.Value)
end)
1 Like

You probably don’t want to be using WaitForChild() multiple times in a loop which runs every 0.5 seconds.

it give me the error attempt to index nil with findfirstchild and Prestige is not a valid member of folder players.alt_paulocezarnbr.leaderstats

(Low contribution)
Have you uploaded the game and enabled datastores?? Cause datastores create instance won’t load(create) if its disabled.

local players = game:GetService("Players")

players.PlayerAdded:Connect(function(player)
	local leaderstats = player:WaitForChild("leaderstats")
	local points = leaderstats:WaitForChild("Points")
	if points then
		local character = player.Character
		local humanoid = character:FindFirstChild("Humanoid")
		task.spawn(function()
			while task.wait(0.5) do
				humanoid.WalkSpeed = points.Value / 3.14
			end
		end)
	end
end)

yes i published and enabled data stores and api

Attempt to index nil with ‘FindFirstChild’

local players = game:GetService("Players")

players.PlayerAdded:Connect(function(player)
	local leaderstats = player:WaitForChild("leaderstats")
	local points = leaderstats:WaitForChild("Points")
	if points then
		local character = player.Character or player.CharacterAdded:Wait()
		local humanoid = character:WaitForChild("Humanoid")
		task.spawn(function()
			while task.wait(0.5) do
				humanoid.WalkSpeed = points.Value / 3.14
			end
		end)
	end
end)
1 Like

Now this is working and issued servers are fixed thanks