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 4 years ago
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)
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 — 4y
0
Please show your leader board script. Soban06 410 — 4y
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 — 4y
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 — 4y

3 answers

Log in to vote
0
Answered by 4 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 — 4y
Ad
Log in to vote
0
Answered by
JesseSong 3916 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

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)


0
If my answer helped please accept it. JesseSong 3916 — 4y
0
Wow, I've grown so much in knowledge over the year. Look how bad I was as a beginner answerer. JesseSong 3916 — 2y
Log in to vote
0
Answered by
0msh 333 Moderation Voter
4 years ago
Edited 4 years ago
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)

Answer this question