I’m trying to make a hunger bar that slowly goes down overtime and is replenished using a text button.
I’ve already made the GUI and the system and it can be replenished using a food tool. But I want it to be replenished using a Text Button.
The problem is, I can’t quite figure out how to convert the eating function from the tool to the TextButtons local script
This is the script for the hunger system overall (ServerScriptService)
There is also a remote event called ‘‘UpdateHunger’’ in replicated storage
And a Bindable event called ‘‘PlayerEats’’
Players = game:GetService("Players")
Maxhunger = 100
Increment = 1 --how much the bar goes down--
Lenght = 30 --How much time needed for the bar to go down--
UpdateHunger = game:GetService("ReplicatedStorage"):WaitForChild("UpdateHunger")
PlayerEats = game:GetService("ReplicatedStorage"):WaitForChild("PlayerEats")
Players.PlayerAdded:Connect(function(Player)
local NewHunger = Instance.new("IntValue")
NewHunger.Value = Maxhunger
NewHunger.Name = "Hunger"
NewHunger.Parent = Player
end)
function ReduceHunger()
local listofplayers = Players:GetChildren()
for I, Player in pairs(listofplayers) do
Player.Hunger.Value -= Increment
if Player.Hunger.Value < 0 then
Player.Hunger.Value = 0
Player.Character.Humanoid.Health -= Increment
end
UpdateHunger:FireClient(Player , Player.Hunger.Value / Maxhunger)
end
end
PlayerEats.Event:Connect(function(Player , Amount)
Player.Hunger.Value += Amount
if Player.Hunger.Value > Maxhunger then
Player.Hunger.Value = Maxhunger
end
UpdateHunger:FireClient(Player , Player.Hunger.Value / Maxhunger)
end)
while true do
wait(Lenght)
ReduceHunger()
end
Also this is the script I used for the food tool.
Tool = script.Parent
Player = Tool.Parent.Parent
FoodValue = 20 --how much the tool restores hunger--
PlayerEats = game:GetService("ReplicatedStorage"):WaitForChild("PlayerEats")
Tool.Activated:Connect(function()
PlayerEats:Fire(Player , FoodValue)
end)
And now the script I put in the TextButton:
--everything is the same except i changed the activation of the tool to mousbutton1click--
Button = script.Parent.Parent
Player = Button.Parent.Parent
FoodValue = 20
PlayerEats = game:GetService("ReplicatedStorage"):WaitForChild("PlayerEats")
script.Parent.MouseButton1Click:Connect(function()
PlayerEats:Fire(Player , FoodValue)
end)
The local script inside the bar itself so it has a tween animation when it depleads.
local HungerBar = script.Parent
local Bar = HungerBar.Bar
local Label = HungerBar.TextLabel
local UpdateHunger = game:GetService("ReplicatedStorage"):WaitForChild("UpdateHunger")
UpdateHunger.OnClientEvent:Connect(function(Value)
Bar:TweenSize(UDim2.fromScale(Value , 1))
Label.Text = "Hunger :" .. tostring(Value * 100) .. "%"
end)
There were no errors in the output. And it would help a lot if someone tried to explain me what I did wrong here DO NOT WRITE YOUR OWN SCRIPTS (i want to try doing the scripting myself)