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

Is .Touched Event broken or did I do something wrong? Need help!

Here's my script;

local Sword = script.Parent
local Hitting = false
local Dmg = script.Parent.Parent:WaitForChild('Stats').Damage

Sword.Touched:Connect(function(hit)
	print('TOUCHED THE GOD')
	if hit.Parent:FindFirstChild('Humanoid') then
		if Hitting == false then
			Hitting = true
			if Hitting == true then
				return
			else
				local Enemy = hit.Parent:FindFirstChild('Humanoid')
				Enemy.Health = Enemy.Health - Dmg.Value
				wait(2)
				Hitting = false
			end
		end
	end
end)

This is a Server Script inside of a part inside of a model.

Stats is a folder inside the model.

Inside stats we have the stats (duh) they are number values.

I'm not sure if I did something wrong but It's not doing damage.

Thank you for any feed back support and help.

1 comment

0

You should use GetPlayerFromCharacter(hit)

2 answers

✓ Accepted answer

I don't think there is any reason to use the hitting variable, but I think you needed to add GetPlayerFromCharacter, because the hit.Parent would just be the player, and the humanoid is located inside of the players character.

local Dmg = script.Dmg
script.Parent.Touched:Connect(function(hit) 
	local player = game.Players:GetPlayerFromCharacter(hit.Parent) 
	local hum = player.Character.Humanoid
	hum.Health = hum.Health - Dmg.Value
	end)

Hopefully this works for you. If it did, please upvote and accept answer. If it didn't please comment. Thanks!

2 comments

0

No. The humanoid is located on the player's module under Workspace, not the service, Players.

0

Oh. I see.

I think this is what is broken. On line 10, why did you make a if statement if that value just got immediately changed? That is why it is not working.

1 comment

0

Hold on I will keep this note right here. I'm currently working on something else I will come back after fixing.

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