Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by 2 years ago

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

0
Player.leaderstats, variable is "player" not "Player" jerryisgod29 176 — 2y
0
kk fixed that but it still doesnt work :( larissashih 5 — 2y
0
Put the code into a code block. MarkedTomato 810 — 2y
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. MarkedTomato 810 — 2y

3 answers

Log in to vote
0
Answered by 2 years ago

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!!!

Ad
Log in to vote
0
Answered by 2 years ago

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.

Log in to vote
0
Answered by 1 year ago
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.

Answer this question