Medkit script not working

Hey Everyone,
I’m making a medkit gamepass for my story game that should slowly heal the player to full health. It’s not working. I set my health to 30 and clicked. Nothing happened.
Here is my script:

Medkit = script.Parent
Handle = Medkit:WaitForChild('Handle')
Player = game.Players.LocalPlayer
CoolDown = false
Humanoid = Player.Character.Humanoid

script.Parent.Activated:Connect(function()
	if CoolDown == false and Humanoid.Health < 100 then
		CoolDown = true
		repeat
			Humanoid.Health = Humanoid.Health + 1.5
			wait(.3)
		until Humanoid.Health == 100
		wait(1)
		Medkit:Destroy()
	end
end)

I get no errors. Thanks for any help!

3 Likes

Is that a normal script or LocalScript?
Instead of using a repeat loop, I’d use a for loop

--// Code
script.Parent.Activated:Connect(function()
	local char = script.Parent.Parent
	local plr = game.Players:GetPlayerFromCharacter(char)
	if plr then
		local h = plr.Character.h
		for i = h.Health, h.MaxHealth, 1.5 do
			if h.Health > 0 and h.Health < h.MaxHealth then
				h.Health = i
				wait(1)
			end
		end
	else 
		print "NO PLAYER FOUND"
	end
end

Medkit:Destroy()

I guess this is how I might do it if I were in your shoes.

It’s a local script, should it be a server script?

ig yes, because the server can’t see client changes.

Yes, it have to be ServerScript

1 Like

So switch that LocalScript to a normal script and put it inside the weapon tool. That should work fine.

1 Like

Here you go! Copy a code from my Donut and it should work!
Link: Click Here!
Wait few minutes so ROBLOX can moderate it and make it aviable.

I tried this:

Medkit = script.Parent

--// Code
script.Parent.Activated:Connect(function()
	local char = script.Parent.Parent
	local plr = game.Players:GetPlayerFromCharacter(char)
	if plr then
		local h = plr.Character.Humanoid
		for i = h.Health, h.MaxHealth, 1.5 do
			if h.Health > 0 and h.Health < h.MaxHealth then
				h.Health = i
				wait(1)
			end
		end
		if h.Health < h.MaxHealth then
			Medkit:Destroy()
	end
	else 
		print "NO PLAYER FOUND"
	end
end)

But it doesn’t heal me.

Change the Script to a LocalScript, I just tested it and it works when the code is in a LocalScript. But LocalScript means it only changes the health on the client so you will need a RemoteEvent.

Where should I fire the remote event?

The activated is in a LocalScript so do a :FireServer.

I would suggest using a while loop instead of repeat.
EG.

while CoolDown == false and Humanoid.Health < 100 do
--healing code here.
end

Dumb question; but how do I paste formatted code into the devforum again? Haven’t done that since awhile, I got a solution for OP. :confused:

Button below escape three times the lua then another line and three more of the button below escape(I can’t remember it’s name.).

1 Like

You need a local script and a server script:

Local Script:

--Your Remote Event:
local remoteEvent = tool:WaitForChild("RemoteEvent")

--Connect our function to fire the remote event when the tool is activated:
tool.Activated:Connect(function()
	remoteEvent:FireServer()
end)

Server Script:

local player = tool.Parent.Parent
local remoteEvent = tool:WaitForChild("RemoteEvent")
local canHeal = true

local human = player.Character:WaitForChild("Humanoid")
human:TakeDamage(50) -- For testing purposes

remoteEvent.OnServerEvent:Connect(function(clientFired)
	if clientFired == player and canHeal then
		--^Make sure the client firing the event is
		--the player who has the tool
		
		print("clientFired is the owner of this tool")
		canHeal = false -- Make sure our player can't heal again if they're already
		
		if human.Health ~= human.MaxHealth and human.Health ~= 0 then
		--^Make sure the player needs to heal and isn't dead
			for i = human.Health, human.MaxHealth, 1 do
				wait(0.1)
				human.Health = i
			end
			tool:Destroy() -- destroy after use
		end
		
	end
end)

Workspace Setup:
Capture2

I seriously recommend learning about remote events/functions and how they work in a Roblox game environment, they are absolutely necessary, I’ll link two super informative articles from the robloxdev website:

EDIT: Added a debounce so the player can’t spam click and their health spasms.

3 Likes

The server script is not very good when it comes to multiple players that have a Medkit becuase of the Player variable above. You should put the player variable in a table and then loop through that table to see if clientfired is in the table.

I don’t really get what you’re trying to say, but remote events return the client in a function, and since I already set the player there’s no need to loop over the table but rather check if the clientFired == player. Why should I put the player in a table if it’s the only key in it?

But it supports FilteringEnabled as the client isn’t allowed to change whatever they want.

The script is not suitable for multiple people that have a medkit.

This only runs once so that means the first player who got the medkit will be the variable player, meaning no one else that has a medkit except the first player can pass this if

But it’s in starter player? Therefore each player gets their own medkit (script, remote and all that), where the script inside defines that unique player. Same thing applies if they respawn. This way the player variable is actually the player and not just the first player that joined the game.

Oh sorry didn’t look at the explorer. I thought it was a script in the ServerScriptService.