Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

Why isn't this simple script damaging the player?

Asked by 4 years ago
Edited 4 years ago

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

0
u should put it in workspace and u also need to find the child before you can damage it coolmanHDMI 72 — 4y
0
Is this a local script? skyaz1 72 — 4y
0
"script.Parent.Humanid"... not the character touching it... the character touching it wont be affected..??? greatneil80 2647 — 4y
0
no local script User#29913 36 — 4y
0
i actually meant the cannon shoot User#29913 36 — 4y

2 answers

Log in to vote
1
Answered by 4 years ago

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)
0
but humanoid is not a part User#29913 36 — 4y
0
wdym? youtubemasterWOW 2741 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

Please provide explanation with your answers. Simply posting code does not spread knowledge of integral scripting processes which helps people understand the logic and reasoning behind your answer.

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)
0
that prob dont work but pls try it coolmanHDMI 72 — 4y
0
oki User#29913 36 — 4y
0
ty for your time User#29913 36 — 4y
0
np coolmanHDMI 72 — 4y

Answer this question