Why won't my healing tool work?

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)

you need to change humanoid health on the server, which can be done by firing a remoteevent from the local script when the tool is used

1 Like

ooohhhh, i see! let me try that.

1 Like

Where is the script located? character limit :sob:

1 Like

i think its in the tool

characterlimit

1 Like

I would change

local Character = Player.Character

with

local Character = Player.Character or Player.CharacterAdded:Wait()
1 Like

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)

Localscript

FoodTray.Activated:Connect(function()
	HealingEvent:FireServer()
	FoodTray.TimesUsed.Value = FoodTray.TimesUsed.Value + 1
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)

Fixed a little issue with the healing!

2 Likes

Not really you can access the humanoid from the script in the tool without any remote events. :joy: :melting_face: :wink:

changes made to Humanoid.Health on the client are not replicated to the server

Only if you use a local script! If you just use a script it does!

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)

and let me know if that fixes the issue.

I’ve tried this solution, and so far it seems like it’ll work! I need to test it, though.

1 Like

Hi! I think I can help a little…

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 :)

Revised server-side (Parent = ServerScriptService)

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)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.