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

When I Made Food Script, It Not Disappear When Eated, How Do I Fix This?

Asked by 11 months ago
Edited 11 months ago

When I Was Making Script That Make Sure People Not OverEat Food For My Hunger System, Trouble Happened, Normally, Food Will Disappear After Some Second When Eated, But It Not Disappear When Your Health Is Under 80, Any Help?

Tool = script.Parent
debounce = false

Tool.Activated:connect(function(mouse)
local hum = Tool.Parent:FindFirstChild("Humanoid")
if (hum ~= nil) and not debounce then
debounce = true
wait(2)
hum.Health = hum.Health + 15
local ps = game:GetService("Players")
local player = ps:GetPlayerFromCharacter(hum.Parent)
local hunger = player:FindFirstChild("Hunger")
hunger.Value = hunger.Value + 20
if hunger.Value >= 100 then
hunger.Value = 100
wait(3)
Tool:Remove()
end end end)

Tool.Equipped:connect(function(mouse)
mouse.Icon = "rbxasset://textures\\GunCursor.png"
end)

1 answer

Log in to vote
0
Answered by 11 months ago
Edited 11 months ago

First: a few nitpicks. On line 10, you don't need to be redefining ps every time the tool is activated. You can put that definition on line 3. Use task.wait() instead of wait(). task.wait() is more accurate and favored by ROBLOX.

Your problem occurs on line 14. The tool only is destroyed if the player's hunger is over 100, because of the end statements being put in poorly. This should fix your issue:

Tool = script.Parent
debounce = false
local ps = game:GetService("Players")

Tool.Activated:connect(
    function(mouse)
        local hum = Tool.Parent:FindFirstChild("Humanoid")
        if (hum ~= nil) and not debounce then
            debounce = true
            task.wait(2)
            hum.Health = hum.Health + 15
            local player = ps:GetPlayerFromCharacter(hum.Parent)
            local hunger = player:FindFirstChild("Hunger")
            hunger.Value = hunger.Value + 20
            if hunger.Value >= 100 then
                hunger.Value = 100
            end
        --your issue was here - end wasn't placed correctly.
        task.wait(3)
            Tool:Remove()
        end
    end)

Tool.Equipped:connect(
    function(mouse)
        mouse.Icon = "rbxasset://textures\\GunCursor.png"
end)

0
Thanks For Helping!, I Am Begginer To Scripting But Now I Understanded Why, Take My Respect! Hiktius 7 — 11mo
Ad

Answer this question