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

Can't get this script to work. "8:attempt to perform arithmetic (sub) on userdata and number"?

Hi, I'm new to scripting and are trying to make a mining game. Here is my script, whenever I try to hit the ore this error appears "8:attempt to perform arithmetic (sub) on userdata and number"

local Ore = game.Workspace.Ore.Gold.GoldOre
local Pick = game.StarterPack.Pic
local Health = game.Workspace.Ore.Gold.GoldOre.Health


Ore.Touched:Connect(function(hit)
	if hit.Parent.Name == "Pic" then
		Health = Health - 10
		
		
		if Health == 0 then
			Health = 30
			Ore.Transparency = 1 
			Ore.CanCollide = false
			wait(5)
			Ore.Transparency = 0
			Ore.CanCollide = true
		   end
		end
	end)

Thanks - WhoopsILied

1 answer

✓ Accepted answer

From the looks of things Health is a value holder which would explain your error.

To resolve this you want to access to value indise of Health e.g. Health.Value

local Ore = game.Workspace.Ore.Gold.GoldOre
local Pick = game.StarterPack.Pic
local Health = Ore.Health  -- health is inside ore so just reuse the variable


Ore.Touched:Connect(function(hit)
    if hit.Parent.Name == "Pic" then -- you could also check the health here.
        Health.Value = Health.Value - 10

	-- use <= as multiple touched events may result in a nagative 
        if Health.Value <= 0 then
            Health.Value = 30
            Ore.Transparency = 1 
            Ore.CanCollide = false
            wait(5)
            Ore.Transparency = 0
            Ore.CanCollide = true
	-- should the health be reset here as well?
           end
        end
end)

I am unsure why you need to hold health in a value object. You should be ok having a local veriable for the health.

I hope this helps. Please comment ig you have any other questions about this post.

4 comments

0

oh ok you decide to remove my post so you can get more reputation. all i did was add .value to it.

0

@iuclds no one removed your post. A note was added to your post to explain your answer. Just posting code does not help anyone. Please explain your answer.

0

@kingdom5 Thanks so much for the help but i have one question, how would i reset the health on the script?

0

on line 18 you would just set the health Health.Value = [your value] though I would look at using a local variable if possible.

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