So i made a hunger bar from a tutorial i watched and i’ve modify it so instead of using a tool its a model with a proximity prompt.
Once i did this what happened was that it didnt work no matter what, i have no clue how, not printing, no errors in output. The prompt shows up and everything but it wont work no matter what
Local script (model with proximity prompt in workspace)
local prox = script.Parent.ProximityPrompt
local Eaten = false
prox.Triggered:Connect(function()
if Eaten then
return
end
Eaten = true
game.ReplicatedStorage.Eat:FireServer()
script.Parent.Parent:Destroy()
end)
Script with the onserver (inside server script service)
local decreaseamount = 75
local decrease = 3
local eat = 10
game.Players.PlayerAdded:Connect(function(Player)
local Hunger = Instance.new("IntValue")
Hunger.Name = "Hunger"
Hunger.Value = 100
Hunger.Parent = Player
Player.CharacterAdded:Connect(function()
Hunger.Value = 100
end)
while wait(decreaseamount) do
if
Hunger.Value <= -10 then
Player.Character:BreakJoints()
else
Hunger.Value = Hunger.Value - decrease
end
end
end)
game.ReplicatedStorage.Eat.OnServerEvent:Connect(function(Player)
Player.Hunger.Value += eat
end)
LocalScripts do not run in the Workspace. You would have to switch to a script and use a BindableEvent instead of a RemoteEvent.
ProximityPrompt script:
local prox = script.Parent.ProximityPrompt
local Eaten = false
prox.Triggered:Connect(function(playerTriggered)
if Eaten then
return
end
Eaten = true
game.ReplicatedStorage.Eat:Fire(playerTriggered)
script.Parent.Parent:Destroy()
end)
ServerScriptService script:
local decreaseamount = 75
local decrease = 3
local eat = 10
game.Players.PlayerAdded:Connect(function(Player)
local Hunger = Instance.new("IntValue")
Hunger.Name = "Hunger"
Hunger.Value = 100
Hunger.Parent = Player
Player.CharacterAdded:Connect(function()
Hunger.Value = 100
end)
while wait(decreaseamount) do
if
Hunger.Value <= -10 then
Player.Character:BreakJoints()
else
Hunger.Value = Hunger.Value - decrease
end
end
end)
game.ReplicatedStorage.Eat.Event:Connect(function(Player)
Player.Hunger.Value += eat
end)
You would need to move the local script to a client sided spot. Such as StarterGui or StarterPlayerScripts.
Use game:GetService("ProximityPromptService") and either use a property of the ProximityPrompt. Like ActionText == “Eat” or something along the lines of that or set a Attribute to something unique.
Other than that if you don’t understand still you could always move the code to a Script and use a bindable event.
Side Note: Remote Events are also unsecure and could be used to a hacker / exploiter’s advantage so I advise you to add code inside the server side when getting the data to make sure that player didn’t fire the event through a exploit.