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

why is it saying "attempt to index nil with part" and why is leaderstats.money not a thing?

Asked by 5 years ago
01Part = script.Parent
02player = game.Players.LocalPlayer
03money = player.leaderstats.money -- my problem is in the Output is says "attempt to index nil with 'leaderstats'"  why is it saying this?
04Part.Touched:Connect(function(hit)
05 
06    if hit.Name == "MoneyNow" then
07        wait(0.7)
08        local function dash()
09            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.
10        end
11        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.
12        end
13 
14 
15 
16end)
0
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 thebananashetoldyou 31 — 5y
0
Please show your leader board script. Soban06 410 — 5y
0
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. thebananashetoldyou 31 — 5y
0
https://www.youtube.com/watch?v=5QMMPvNgTR4&feature=youtu.be here i made a youtube video to explain what i need help with thebananashetoldyou 31 — 5y

3 answers

Log in to vote
0
Answered by 5 years ago

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.

0
oh i understand localplayer isnt in regular scripts so i need to get the leaderstats from the player somehow thanks!! (my brain grows now) thebananashetoldyou 31 — 5y
Ad
Log in to vote
0
Answered by
JesseSong 3916 Moderation Voter Community Moderator
5 years ago
Edited 5 years ago

Leaderboard script:

01game.Players.PlayerAdded:connect(function(p)
02    local stats = Instance.new("IntValue")
03    stats.Name = "leaderstats"
04    stats.Parent = p
05 
06    local money = Instance.new("IntValue")
07    money.Name = "Money" -- Change "Money" to anything you want to name it like "Cash"
08    money.Value = 50 -- Change the value to how many you want when the player joins the game
09    money.Parent = stats
10end)
0
If my answer helped please accept it. JesseSong 3916 — 5y
0
Wow, I've grown so much in knowledge over the year. Look how bad I was as a beginner answerer. JesseSong 3916 — 3y
Log in to vote
0
Answered by
0msh 333 Moderation Voter
5 years ago
Edited 5 years ago
1Part.Touched:Connect(function(hit)
2 
3    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

1local function dash()
2            player.leaderstats.money.Value = money.Value + hit.Value
3        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:

01local debounce = false
02 
03Part = script.Parent
04Part.Touched:Connect(function(hit)
05 
06    if hit.Parent:FindFirstChild("Humanoid") then
07    debounce = true
08    local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
09    local money = player:FindFirstChild("leaderstats").money
10 
11        wait(0.7)
12        player.leaderstats.money.Value = money.Value + hit.Value
13        Part:Destroy()
14        end
15 
16 
17 
18end)

Answer this question