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

How do I make this error disappear? Punching

I posted something like this but it never worked so i decided to make a whole new script and now I need your help to make it work

local ReplicatedStorage = game:GetService('ReplicatedStorage')
local PunchEvent = ReplicatedStorage:WaitForChild('Punch')
PunchEvent.OnServerEvent:Connect(function(hi)
	local ctr = hi.Character 
    local hum = ctr.Humanoid
    local LeftArm = ctr:FindFirstChild("Left Arm")
	local Power = hi.StatsFolder.StrengthValue.Value
	LeftArm.Touched:Connect(function(PlayerWhoSent, HumanoidToDamage, Power)
		print("touched")
		if HumanoidToDamage.Parent and HumanoidToDamage.Parent.Humanoid then
			HumanoidToDamage:TakeDamage(Power)
			
		end
	end)
end)

the error is attempt to index local 'HumanoidToDamage' (a nil value) this is a punch script and i have made another script for the punch animation in starter pack if needed then tell me. if i do print(HumanoidToDamage) it prints nil

1 comment

0

Touched event only has 1 parameter which is the PartThatTouched parameter

2 answers

The reason for your error is because the Touched event does not return the values you seem to expect it will

The return value of touch is the part that touched, so you could do

local ReplicatedStorage = game:GetService('ReplicatedStorage')
local PunchEvent = ReplicatedStorage:WaitForChild('Punch')
PunchEvent.OnServerEvent:Connect(function(hi)
	local ctr = hi.Character 
	local hum = ctr.Humanoid
	local LeftArm = ctr:FindFirstChild("Left Arm")
	local Power = hi.StatsFolder.StrengthValue.Value
	LeftArm.Touched:Connect(function(toucher)
	    local humanoid = toucher.Parent and toucher.Parent:FindFirstChild("Humanoid") or nil
		if humanoid then
			humanoid:TakeDamage(Power)
		end
	end)
end)

1 comment

0

idk y but everything is working but the dummy or whatever u call isn't taking damage

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