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

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

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

✓ Accepted answer

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)

1 comment

0

Thanks For Helping!, I Am Begginer To Scripting But Now I Understanded Why, Take My Respect!

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