Part = script.Parent
player = game.Players.LocalPlayer
money = player.leaderstats.money -- my problem is in the Output is says "attempt to index nil with 'leaderstats'" why is it saying this?
Part.Touched:Connect(function(hit)
if hit.Name == "MoneyNow" then
wait(0.7)
local function dash()
player.leaderstats.money.Value = money.Value + hit.Value -- i gave a value to a part called "MoneyNow" and if it touches this part then i wanted it to add its value to the leaderboards-money value which i created.
end
hit:Destroy() -- part gets destroyed after the code happends(im trying to get the tycoon money thing where when a part touches the money make part then it gives money to the player then deletes the part.
end
end)
why is it saying "attempt to index nil with part" and why is leaderstats.money not a thing?
3 answers
If you are using a regular script then LocalPlayer does not exist. Instead, you would need to get the player whenever they touch the part and change their leaderstats from there.
1 comment
oh i understand localplayer isnt in regular scripts so i need to get the leaderstats from the player somehow thanks!! (my brain grows now)
Leaderboard script:
game.Players.PlayerAdded:connect(function(p)
local stats = Instance.new("IntValue")
stats.Name = "leaderstats"
stats.Parent = p
local money = Instance.new("IntValue")
money.Name = "Money" -- Change "Money" to anything you want to name it like "Cash"
money.Value = 50 -- Change the value to how many you want when the player joins the game
money.Parent = stats
end)
2 comments
If my answer helped please accept it.
Wow, I've grown so much in knowledge over the year. Look how bad I was as a beginner answerer.
Part.Touched:Connect(function(hit)
if hit.Name == "MoneyNow" then
unless the part that touched Part is "MoneyNow" then I don't see how this will connect to the player's leaderstats in any way
local function dash()
player.leaderstats.money.Value = money.Value + hit.Value
end
hit.Value is not a value because hit is what touched the part (ex. player character's Right Foot), and I don't understand why is there a local function out of nowhere.
local scripts does not work in workspace, use a regular script instead. here would be a more correct way to do it:
local debounce = false
Part = script.Parent
Part.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
debounce = true
local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
local money = player:FindFirstChild("leaderstats").money
wait(0.7)
player.leaderstats.money.Value = money.Value + hit.Value
Part:Destroy()
end
end)
This discussion is preserved as it was. New answers, comments, and votes are closed.

4 comments
i also wanted to know why the script doesn't know what "money" is I put it as a child to leaderstats and its saying it's not defined but it should be since i put it in leaderstats as a child
Please show your leader board script.
thanks for the answers!!, but wait lemme make a video to show what I mean because i'm not the thing touching the part another part is.
https://www.youtube.com/watch?v=5QMMPvNgTR4&feature=youtu.be here i made a youtube video to explain what i need help with