this is the script :
CD = script.Parent.ClickDetector local player = game.Players.LocalPlayer
CD.MouseClick:Connect(function(click) if click.Humanoid.Health < 100 then click.Humanoid.Health = click.Humanoid.Health + 30 end end)
ik it does not work i am new at scripting i want to learn it plz help
Humanoid isn't inside of a player, its inside the character.
script.Parent.ClickDetector.MouseClick:Connect(function(player) local char = player.Character or player.CharacterAdded:Wait() if char.Humanoid.Health < 100 then char.Humanoid.Health = char.Humanoid.Health + 30 end end)
CD = script.Parent.ClickDetector --if the scripts parent is the click detector script.Parent already references it if the script's parent is the mode this is ok. local player = game.Players.LocalPlayer --the "LocalPlayer" can only be referenced in a local script, and a local script can't change the player's health or anything else serverside CD.MouseClick:Connect(function(click) --the first argument inside function which you called click references the player object already so "LocalPlayer" is unneccessary if click.Humanoid.Health < 100 then --click references the player object not the model in workspace so click has no child called "Humanoid" click.Humanoid.Health = click.Humanoid.Health + 30 --same problem trying to change the value your referencing nil end end)
Ok now that all the issues in your script are show this is how to fix it:
script.Parent.MouseClick:Connect(function(player) -- the script's Parent is the ClickDetector so even if the parent of click detector changes the script should still work local human=player.Character:FindFirstChild("Humanoid") if human ~=nil and human.Health<human.MaxHealth then -- checks if humanoid exist and has less than the max hp human.Health=human.Health+30 --adds 30 to the humanoid's health print(human.Parent.Name) end end)
Also if you new to scripting you should go for simple stuff first, as going directly for complicated topics only helps to frustrate and discourage you.For Example when I just started I did simple things like building volcanoes, scripting traffic lights, playing sounds, making doors with click detectors, and similiar things before aiming to make actual games.
Also use error messages to fix your script, you can copy and paste error messages into google which will help you understand what they mean.Finally make sure to read over your script for incorrect indexing, indexing is trying to reference an object for example "
game.Workspace.Object
", so don't incorrectly spell it like "game.Workspace.object
" make sure the capitlization and spelling are exact. Also if you want to reference something that has a space in it's name without an error then do this "game.Workspace["Ob ject"]
" a string inside square brackets without a ' . ' before it