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

How can I script a tool to be unusable while under a certain condition?

Asked by
Hypgnosis 186
5 years ago

I've made a tool to recover the value of an IntValue (HungerVal.Value). However, I need this tool to only be usable when the player's thirst and hunger IntValues are above 0. The below code still lets me use the tool when either of those IntValues equal 0, when it should instead be unusable while that condition is true.

local food = script.Parent
local plr = game.Players.LocalPlayer
local HungerVal = plr:WaitForChild("HungerVal")
local ThirstVal = plr:WaitForChild("ThirstVal")
local used = false

food.Activated:connect(function()

    if HungerVal.Value and ThirstVal.Value > 0 then
        HungerVal.Value = HungerVal.Value + 40
        used = true

        if HungerVal.Value > 100 then
            HungerVal.Value = 100
        end

        food:Destroy()

    else
        print("I can't do that! I am unconcious!")
    end
end)

1 answer

Log in to vote
0
Answered by 5 years ago

The problem is that you are not checking HungerVal's Value. You are just checking if HungerVal.Value exist. so ofcource is gonna be true. So to fix this you would check both Values.

if HungerVal.Value > 0 and ThirstVal.Value > 0 then
    -- Code
end
0
I see whats wrong now. Thanks. Hypgnosis 186 — 5y
0
Your welcome! MRbraveDragon 374 — 5y
Ad

Answer this question