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

ServerScriptService.Script:6: attempt to index local 'humanoid' (a nil value) how to fix ?

I've been trying to fix this for hours, i'm a beginner at this stuff

LocalScript:

local plr = game:GetService("Players").LocalPlayer

local char = plr.Character or plr.CharacterAdded:wait()

local mouse = plr:GetMouse()

local tool = script.Parent

local anim = tool:FindFirstChild("PunchAnimation")

local Debounce = true

local Damaged = false



char:WaitForChild("LeftHand").Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") and not Debounce and not Damaged and hit.Parent.Humanoid ~= plr.Character.Humanoid then
		if plr.Character.Humanoid.Health > 0 then
			Damaged = true
			game:GetService("ReplicatedStorage").RemoteEvents.punch
:FireServer(hit.Parent.Humanoid)
		end
	end
end)






tool.Equipped:Connect(function()
	
    print("A tool was equipped")
    mouse.Button1Down:Connect(function()
	
	
	
	
	print("Tool clicked")
	local hum = tool.Parent:FindFirstChild("Humanoid")
	if hum and Debounce then
		Debounce = false
		anim = hum:LoadAnimation(tool.PunchAnimation)
		anim:Play()
		wait(0.5)
		Debounce = true
	
		
	end
	
	

end)
end)

ServerScript:

game:GetService("ReplicatedStorage")
.RemoteEvents.punch.OnServerEvent:Connect(function(player, humanoid)
	
	if humanoid.Health >= 0 then
		humanoid.Health = humanoid.Health - 10
	elseif humanoid.Health < 0 then
		humanoid.Health = 0
	end
	
	
	
	
end)

1 answer

You shouldn't be trusting the client with that type of information but, the humanoid you're referring to in the server script is nil

5 comments

0

yes i get it but how do you fix it, ik that i should't trust the client but im still learning all this stuff and these remote events are the most difficult things for me

0

Hmm I guess try FindFirstChild? This is very odd as you checked for the Humanoid beforehand but, trying it won't hurt I guess.?

0

Also, regularly you would want to do the animation on the Client then on the Server you'd want to do damage.

0

Which means just do the .Touched function on the Server even though I don't recommend using .Touched for this.

0

tried to put hit.Parent:FindFirstChild("Humanoid") in the fireserver but didnt work same error WaitForChild aswell

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