Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why isn't this simple script damaging the player?

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

5 comments

0

u should put it in workspace and u also need to find the child before you can damage it

0

Is this a local script?

0

"script.Parent.Humanid"... not the character touching it... the character touching it wont be affected..???

0

no local script

0

i actually meant the cannon shoot

2 answers

Hello.

Problem

You used Part.Touched. Instead, use Humanoid.Touched. It will fire when you touch a part.

Recommendations

  1. Write local before declaring a function.

  2. 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)

2 comments

0

but humanoid is not a part

0

wdym?

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)

4 comments

0

that prob dont work but pls try it

0

oki

0

ty for your time

0

np

This discussion is preserved as it was. New answers, comments, and votes are closed.