Leaderstats folder

Hello,

recently i have made a leaderstats folder for my upcoming game. When i try to access this from another script it prints out “unable to index nil with leaderstats”, any help. I know that i have spelt leaderstats right because i have checked both scripts.

2 Likes

How are you referring to the player in the script? Based on the error it seems like there’s something wrong with that.

2 Likes

i’m using

player:WaitForChild(“leaderstats”)

1 Like

But how are you getting the value for the player variable?

2 Likes

local player = game.Players.LocalPlayer

1 Like

Is it a server script? If it is, you can’t use Players.LocalPlayer, but you can use Players.PlayerAdded. PlayerAdded gives the player as an argument.

2 Likes

i will try using PlayerAdded and i am using this in a server script. To give this more detail what i want to do is when a player touches a part it adds 100 Cash to a value and then saves it if that helps. I will try it now.

1 Like

Would you mind sharing your current script, so it would be easier for us to help you?

2 Likes

This is the edited script using PlayerAdded

local DataStoreService = game:GetService(“DataStoreService”)
local DataStore = DataStoreService:GetDataStore(“Bloks”)

game.Players.PlayerAdded:Connect(function(player)
local leaderstats = player:WaitForChild(“leaderstats”)
local value = leaderstats:WaitForChild(“bloks”)
script.Parent.Touched:Connect(function()
value.Value = value.Value +100
DataStore:UpdateAsync(player.UserId, value.Value)
end)
end)

1 Like
local Part = script.Parent
local Amount = 100

Part.Touched:Connect(function(Hit)
    if game.Players:GetPlayerFromCharacter(Hit.Parent) then
         local Player = game.Players:GetPlayerFromCharacter(Hit.Parent) 
         Player:WaitForChild("leaderstats").Cash.Value = Player:WaitForChild("leaderstats").Cash.Value + Amount
    end
end)

Try putting this inside your brick you want the player to touch and get cash.

2 Likes

I also wouldn’t suggest updating the datastore every time someone touches the part

1 Like

the odd thing is is when i join the game and i look into the leaderstats at the top playerlist, it says the name is “Value” but when i check the explorer it says the value is bloks (the currency name)

Can you post your leaderstats script so we can see it

1 Like

local DataStoreService = game:GetService(“DataStoreService”)
local DataStore = DataStoreService:GetDataStore(“Bloks”)

game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new(“Folder”, player)
leaderstats.Name = “leaderstats”
local bloks = Instance.new(“IntValue”, leaderstats)
bloks.Value = DataStore:GetAsync(player.UserId, bloks.Value)
bloks.Name = “bloks”

game.Players.PlayerRemoving:Connect(function(player)
	DataStore:SetAsync(player.UserId, bloks.Value)
end)

end)

local RunService = game:GetService("RunService")
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("Bloks")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	local bloks = Instance.new("IntValue")
	bloks.Value = 0
	bloks.Name = "bloks"
	bloks.Parent = leaderstats

	if not RunService:IsStudio() then
		local data
		local success, err = pcall(function()
			data = DataStore:GetAsync(player.UserId)
		end)

		if success then
			if data then
				bloks.Value = data
			end
		else
			player:Kick("Failed to load data, please rejoin")
		end
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	pcall(function()
		DataStore:SetAsync(player.UserId, player.leaderstats.bloks.Value)
	end)
end)
2 Likes

thanks, ill test this now in studio.

when i added this to studio, it wont create a leaderstats.

All credit to @VortexD3V for the script.

You would also want to add a :BindToClose in case you shut down a server so it would also save then.

1 Like