I was working on a script that would allow the part to give health points to players but apparently this error
broke the script(you can take a look at the video to know what im talking about):
Here’s the script for it aswell
script.Parent.Touched:Connect(function(hit)
local player = hit.Parent.Name
local newHealth = 30
if player then
player.Health = player.Health + newHealth
end
script.Parent:Destroy()
end)
script.Parent.Touched:Connect(function(hit)
local humanoid = hit.Parent:FindFirstChild(“Humanoid”)
local new health = 30
if humanoid then
humanoid.Health = humanoid.Health + newHealth
end
script.Parent:Destroy()
end)
Your mistake was that you were giving more life to the player’s name, instead of the Humanoid which has Health as its property.
You should make sure you’re actually referencing the Humanoid when changing health. It looks like you’re just trying to set the health of the string hit.Parent.Name.
Here’s a revised bit of code to hopefully ensure that you are referencing the player’s humanoid:
local newHealth = 30
script.Parent.Touched:Connect(function(hit)
local Model = hit:FindFirstAncestorOfClass("Model")
if Model then
local Humanoid = Model:FindFirstChild("Humanoid")
if Humanoid then
Humanoid.Health = Humanoid.Health + newHealth
script.Parent:Destroy()
end
end
end)
This code ensures that a humanoid exists before trying to perform any kind of math on the health of the object.
What line of the following code is it yielding that error?
local newHealth = 30
script.Parent.Touched:Connect(function(hit)
local Model = hit:FindFirstAncestorOfClass("Model")
if Model then
local Humanoid = Model:FindFirstChild("Humanoid")
if Humanoid then
Humanoid.Health = Humanoid.Health + newHealth
script.Parent:Destroy()
end
end
end)
It’s odd to receive an error like that when I’m performing checks to prevent them.
I just loaded the place file and it worked exactly as intended. I’m not sure how you’re receiving that error as there’s nothing on Line 2 of the WoodCrate script and it runs fine for me.
I created a two player testing session with the place file you provided to me. Did you publish that place file to the website when testing it on the Roblox client?