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

Attempt to preform Arithmetic on Nil Value?

Asked by 10 years ago

I have this script that when the thing is eaten, a health bar goes down.

I get this error

22:12:17.349 - Workspace.Water2.WaterScript:25: attempt to perform arithmetic on global 'currentthirst' (a nil value)

This is the script:

local Tool = script.Parent;
local enabled = false

function onActivated()
    enabled = false
    Tool.GripForward = Vector3.new(0,-.759,-.651)
    Tool.GripPos = Vector3.new(1.5,-.5,.3)
    Tool.GripRight = Vector3.new(1,0,0)
    Tool.GripUp = Vector3.new(0,.651,-.759)
    Tool.Handle.DrinkSound:Play()
    script.Parent.Bites.Value = script.Parent.Bites.Value + 1
    Eable()
    delay(3,function()
        local character = Tool.Parent
        local player = game.Players:GetPlayerFromCharacter( character )
        thirst = player.PlayerGui.Thirst
        maxthirst = 100 --create a max thirst value
        currentthirst = 100 --create a currentthirst value
        local currentthirstpercent = currentthirst/maxthirst 
    end)
end


function updatebar()
    local currentthirstpercent = currentthirst/maxthirst --re-divides the thirst values
    thirst.BG.Bar.Size = UDim2.new(currentthirstpercent,0,1,0) --sets the size in the x of the bar to the percentage of thirst remaining
end

function drink() --example function for increasing thirst
    currentthirst = currentthirst + -50 --add thirst together
    if currentthirst > maxthirst then --checks to see you aren't over the max
        currentthirst = maxthirst --if you're over the max it sets you to the max
    end
end

updatebar() --updates the bar because you changed the value of thirst

Tool.GripForward = Vector3.new(-.976,0,-0.217)
Tool.GripPos = Vector3.new(0.03,0,0)
Tool.GripRight = Vector3.new(.217,0,-.976)
Tool.GripUp = Vector3.new(0,1,0)
enabled = true



function onEquipped()
    Tool.Handle.OpenSound:play()

end

function Eable()
if (script.Parent.Bites.Value) > 5 then 
script.Parent:Remove()
end
end

script.Parent.Activated:connect(onActivated)
script.Parent.Equipped:connect(onEquipped)

2 answers

Log in to vote
0
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
10 years ago

You never defined 'currentthirst' on line 25 in your script

if I say..

print(oneplusone)

'oneplusone' will return nil because it was never defined.. DEFINE IT.

local oneplusone = 2
print(oneplusone)
--Will print '2'
0
Isn't it defined in line 18? SpazzMan502 133 — 10y
0
line 18 is inside the scope of a function. For it to be defined you would have to have called the function as a prerequisite for line 25 to not break your code. Goulstem 8144 — 10y
Ad
Log in to vote
0
Answered by
Ryzox 220 Moderation Voter
10 years ago

Line 51 you are declaring the function "Eable()" underneath where you try to state the function. Try this instead:

function Eable()
if (script.Parent.Bites.Value) > 5 then
script.Parent:Remove()
end
end

function onActivated()
    enabled = false
    Tool.GripForward = Vector3.new(0,-.759,-.651)
    Tool.GripPos = Vector3.new(1.5,-.5,.3)
    Tool.GripRight = Vector3.new(1,0,0)
    Tool.GripUp = Vector3.new(0,.651,-.759)
    Tool.Handle.DrinkSound:Play()
    script.Parent.Bites.Value = script.Parent.Bites.Value + 1
    Eable() -- Here this is being called before it was defined I suggest putting function Eable() at the top
    delay(3,function()
        local character = Tool.Parent
        local player = game.Players:GetPlayerFromCharacter( character )
        thirst = player.PlayerGui.Thirst
        maxthirst = 100 --create a max thirst value
        currentthirst = 100 --create a currentthirst value
        local currentthirstpercent = currentthirst/maxthirst
    end)
end

And then declaring it above where you define the function. Check that out.

Answer this question