I'm making a cannon for my game. It's not sensing that the player is touching it. Why is that so? code:
function touch(hit) if hit.Name == "Part" then script.Parent.Humanoid.Health = script.Parent.Humanoid.Health - 25 print("changed health") end end script.Parent.UpperTorso.Touched:Connect(touch)
The code is in game.StarterPlayer.StarterCharacterScripts
Hello.
Problem
You used Part.Touched
. Instead, use Humanoid.Touched
. It will fire when you touch a part.
Recommendations
Write local
before declaring a function.
Use the Humanoid:TakeDamage()
function as it won't damage you if you have a ForceField.
Fixed Code
local function onTouched(hit) if hit.Name == "Part" then script.Parent.Humanoid:TakeDamage(25) print("changed health") end end script.Parent.Humanoid.Touched:Connect(onTouched)
like this
function hit() if hit.Name == "Part" then local human = script.Parent:FindFirstChild("Humanoid") if (human ~= nil) then human:TakeDamage(25) end end script.Parent.UpperTorso.Touched:Connect(hit)