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

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

Asked by 4 years ago

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

Log in to vote
1
Answered by 4 years ago

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.

0
oh ok you decide to remove my post so you can get more reputation. all i did was add .value to it. iuclds 720 — 4y
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. User#5423 17 — 4y
0
@kingdom5 Thanks so much for the help but i have one question, how would i reset the health on the script? WhoopsILied 7 — 4y
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. User#5423 17 — 4y
Ad

Answer this question