Hey guys i made this script earlier today and its meant to work as a healing pad for players when they touch it.
local Medkit = script.Parent local function onPartTouch(hit) local Player = hit.Parent:WaitForChild("Humanoid") local hp = Player.Health local maxhp = Player.MaxHealth -- local healAmount = (maxhp - hp) if Player then hp = maxhp --(Player.Health + healAmount) print ("hit") wait (1) end end Medkit.Touched:Connect(onPartTouch)
It detects the player correctly as i get the print in the output however it doesn't change the players health. I have compared my script with other peoples functioning versions but I still don't understand the problem with mine. Any help would be much appreciated, thanks.
I'm revising my answer with the hopes that it will help you learn.
local Medkit = script.Parent local Debounce = true local function onPartTouch(hit) if (not Debounce) then return; end --//This is a part of the debounce, we're setting a debounce, that just allows the part to be touched every one second local Humanoid = hit.Parent:FindFirstChild("Humanoid") --//We see if we can find the Humanoid inside the parent of the part that touches the medkit if (Humanoid) then --//We found it Debounce = false --//We're setting the debounce to false to the touched event can no longer fire while we do what we need Humanoid.Health = Humanoid.MaxHealth --//Setting the health to the maxhealth of the humanoid wait(1) --//You can change this to however long you want to wait before touching the medkit again to get more health Debounce = true --//Changing the debounce to true so we can touch the part again end end Medkit.Touched:Connect(onPartTouch)
I've added in a lot of comments, I hope you'll read them. All I'm doing is optimizing your code and this should do exactly what you want it to do. If you have any more problems just tell me.
okay so i think the issue is that you want to change the health of the PLAYER not the HUMANOID wich contains the 'helath' property. First you have to get the character and then the humanoid
Please consider accepting my answer by pressing the 'accept answer' button and hit me up through the comments if you need help have a nice day