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

leaderboards not working, i get error code attempt to index nil with 'leaderstats'?

im trying to make it so everytime you click a part it drops a money meshpart behind it, and when you touch the meshpart it gives you +1 money on leaderboards with datastores. so far ive almost finished it, but i cant seem to figure out how to make it give you money on the leaderboards. i always get the error code "Workspace.Money.Script:6: attempt to index nil with 'leaderstats'" here is the code for the datastore and the leaderboard:

--datastore local DataStoreService = game:GetService("DataStoreService") local Players = game:getService("Players")

local MoneyData = DataStoreService:GetDataStore("MoneyData")

local CurrencyName = "Money" local StartingValue = 0

Players.PlayerAdded:Connect(function(player) local UserData local success, errMsg = pcall(function() UserData = MoneyData:GetAsync(player.UserId) end)

if success == false then
	local doNotSave = Instance.new("Folder")
	doNotSave.Name = "DoNotSave"
	doNotSave.Parent = player
else
	print("Data Is Loaded!!")
end

local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Currency = Instance.new("IntValue")
Currency.Name = CurrencyName
Currency.Value = UserData or StartingValue
Currency.Parent = leaderstats

end)

--leaderboard Players.PlayerRemoving:Connect(function(player) local SavingPath = player.leaderstats:FindFirstChild(CurrencyName)

if player:FindFirstChild("DoNotSave") then
	warn("Player data was not saved to avoid data loss.")
else
	MoneyData:SetAsync(player.UserId, SavingPath.Value)
end

end)

here is the money script:

local Part = script.Parent

Part.Touched:Connect(function(hit) if hit.Parent:FindFirstChild("Humanoid") then local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent) Player.leaderstats.Money.Value = Player.leaderstats.Money.Value + 1 Part:Destroy() end end)

the datastore script is inside of serverscriptservice and the money script is inside of the money meshpart in replicated storage

4 comments

0

Player.leaderstats, variable is "player" not "Player"

0

kk fixed that but it still doesnt work :(

0

Put the code into a code block.

0

In the touch function, you made a variable for the Player and named it "player" with a lowercase p, but you did "Player.leaderstats.Money.Value" with an uppercase p. This error happened because Roblox lua is case sensitive and there was no variable named "Player" meaning nil. So your code was basically this "nil.leaderstats.Money.Value" which throws an error.

3 answers

Here is what the money script should look like: First, put a click detector in the part. Inside the click detector, and put this:

local plr = game.Players.LocalPlayer

script.Parent.MouseClick:Connect(function()
	plr.leaderstats.Currency.Value = plr.leaderstats.Currency.Value + 1
end)

I am new to scripting, so I hope. this works. Good luck!!!

As kickoff127 said, you will need a click detector. The script should look like this:

local plr = game.Players.LocalPlayer

script.Parent.MouseClick:Connect(function()
    plr.leaderstats.Currency.Value = plr.leaderstats.Currency.Value + 1
end)

The leaderstats needs to be binded with the object/script.

local Part = script.Parent

Part.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        local Player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent) 

        if Player then
            Player.leaderstats.Money.Value += 1
            Part:Destroy()
        end
    end
end)

What happens is that when something touches the part it fires, it verifys if there is an Humanoid, but it can be finded but not be a player.

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