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

attempt to perform arithmetic (add) on nil and number?

Asked by 2 years ago

local CashValue = script.Parent.Parent.Parent.Parent.Values.CashValue

script.Parent.Touched:Connect(function(Hit)
    if Hit.Name == "Part" and Hit:FindFirstChild("CashValue") then
        CashValue.Value += Hit:FindFirstChild("CashValue").Value
        Hit:Destroy()
    end
end)

1 answer

Log in to vote
0
Answered by 2 years ago
Edited 2 years ago

Are you trying to do this to increase a cash of a player?

This is just an explanation, if you want the fix, go to the end of this post.

If so, Hit is just the part that hits it. If a player character touched it, Hit would be the limb that touched it. So to identify the character, it would be like:

script.Parent.Touched:Connect(function(hit)
    local char = hit.Parent
end

We just got the character, yay! But of course, it'd be useless without codes. To print out the name of the player, you can print the Name of the character or the Player.

But.. how do I get the player?

Easy! We got our character, now let's identify the player.

script.Parent.Touched:Connect(function(hit)
    local char = hit.Parent
    local plr = game.Players:GetPlayerFromCharacter(char)

    if plr then
        -- bla
    end
end

Players:GetPlayerFromCharacter() is pretty self-explanatory. You can get the player that touched the part by using the character to identify it. And I used an if statement to check if that player exists.

Now, to increase the money:

script.Parent.Touched:Connect(function(hit)
    local char = hit.Parent
    local plr = game.Players:GetPlayerFromCharacter(char)

    if plr then
        local lead = plr.leaderstats -- Assuming it's a leaderstats
        local cash = lead.CashValue

        cash.Value += 1
    end
end

Voila! We just increased the cash of the player.

Ad

Answer this question