Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.