I’ve been trying to get a tray to heal you upon a limited amount of usage, but, the script in which I’ve tried out isn’t working as intended. In fact, it isn’t doing much of anything unfortunately. I’m doing this within a localscript, to let you know. Below you’ll find the piece of erroneous scripting.
Local,script
local FoodTray = script.Parent
local Player = game.Players.LocalPlayer
local Character = Player.Character
local Humanoid = Character:WaitForChild("Humanoid")
local Healing = false
FoodTray.Activated:Connect(function()
if Healing == false then
Healing = true
repeat wait(1) Character:WaitForChild("Humanoid").Health += 20 until Character:WaitForChild("Humanoid").Health >= Humanoid.MaxHealth
FoodTray.TimesUsed.Value = FoodTray.TimesUsed.Value + 1
end
end)
I’ve tried transferring my script over to the serverside, however, it doesn’t seem to be healing… The TimesUsed system works, however.
Serverscript
local replicatedStorage = game:GetService("ReplicatedStorage")
local healingEvent = replicatedStorage.HealingEvent
local Healing = false
healingEvent.OnServerEvent:Connect(function(player)
local Character = player.Character
local humanoid = Character:WaitForChild("Humanoid")
if Healing == false then
Healing = true
repeat wait(1) Character:WaitForChild("Humanoid").Health += 20 until Character:WaitForChild("Humanoid").Health >= humanoid.MaxHealth
end
end)
This script heals you 15 health each time you use it. Just make a script inside the tool and paste the code.
local tool = script.Parent
local db = false
tool.Activated:Connect(function()
if db then return end
db = true
local hum = script.Parent.Parent:FindFirstChild("Humanoid")
hum:TakeDamage(-15)
print("Healed")
wait(1)
db = false
end)
Your script will work, but only once. You’re checking if the “Healing” boolean is false, and only running your script on activation if that is the case, but after setting the boolean to true while running the script you never set it back to false and the script never runs again.
Try this out -
local FoodTray = script.Parent
local Player = game.Players.LocalPlayer
local Character = Player.Character
local Humanoid = Character:WaitForChild("Humanoid")
local Healing = false
FoodTray.Activated:Connect(function()
if Healing == false then
Healing = true
repeat wait(1) Character:WaitForChild("Humanoid").Health += 20 until Character:WaitForChild("Humanoid").Health >= Humanoid.MaxHealth
FoodTray.TimesUsed.Value = FoodTray.TimesUsed.Value + 1
wait(1)
Healing = false
end
end)
First of all, assuming @bIIocky’s newest script is his newest reply, I’ll fix up some things both on the client and the server.
Server-side evaluation
local replicatedStorage = game:GetService("ReplicatedStorage")
local healingEvent = replicatedStorage.HealingEvent
local Healing = false --[[tiny mistake, assuming you plan to put this in ServerScriptService,
(which you should)
this would make any other players firing the remote unable to heal
if one player is going through the function.
After looking at the function, it appears you need a cooldown for each player.
I'll help in my version of your server-side code
]]
healingEvent.OnServerEvent:Connect(function(player)
local Character = player.Character -- local character = player.Character or player.CharacterAdded:Wait() 😉
local humanoid = Character:WaitForChild("Humanoid")
if Healing == false then
Healing = true
--for loops use task.wait()
repeat wait(1) Character:WaitForChild("Humanoid").Health += 20 until Character:WaitForChild("Humanoid").Health >= humanoid.MaxHealth
^ cool :o,
end
end)
Client-side evaluation
FoodTray.Activated:Connect(function()
HealingEvent:FireServer()
FoodTray.TimesUsed.Value = FoodTray.TimesUsed.Value + 1 -- exploiters go brrr
end)
--just handle the times used & deletion on the server :)
local replicatedStorage = game:GetService("ReplicatedStorage")
local healingEvent = replicatedStorage.HealingEvent
local playersHealing = {}
healingEvent.OnServerEvent:Connect(function(player)
print("Checkpoint2")
local Character = player.Character or player.CharacterAdded:Wait()
local humanoid = Character:WaitForChild("Humanoid")
print("Checkpoint3")
if playersHealing[player.UserId] == false or nil then
print("Checkpoint4")
playersHealing[player.UserId] == true
repeat
task.wait(1)
print("Healing")
humanoid.Health += 20
until humanoid.Health >= humanoid.MaxHealth
print("Checkpoint5")
playersHealing[player.UserId] == false
end)
Revised client-side (Parent = tool)
local replicatedStorage = game:GetService("ReplicatedStorage")
local healingEvent = replicatedStorage.HealingEvent
FoodTray.Activated:Connect(function()
print("Checkpoint1")
HealingEvent:FireServer()
--FoodTray.TimesUsed.Value = FoodTray.TimesUsed.Value + 1 (handle this on the server, and don't send it through the remote)
end)
I’m too lazy to test I’ll be honest, but if there’s an error you’re unable to solve by yourself reply and I’ll give it another shot. I’ve added prints in case it doesn’t work but you see no error.
Edits: forgot to capitalize the C in character (in revised)