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