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

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

Asked by 4 years ago

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)

Closed as Non-Descriptive by hiimgoodpack, EmbeddedHorror, and BlackOrange3343

This question has been closed because its title or content does not adequately describe the problem you are trying to solve.
Please ensure that your question pertains to your actual problem, rather than your attempted solution. That is, you were trying to solve problem X, and you thought solution Y would work, but instead of asking about X when you ran into trouble, you asked about Y.

Why was this question closed?

1 answer

Log in to vote
0
Answered by
AizakkuZ 226 Moderation Voter
4 years ago
Edited 4 years ago

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

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 EvilTony99 -7 — 4y
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.? AizakkuZ 226 — 4y
0
Also, regularly you would want to do the animation on the Client then on the Server you'd want to do damage. AizakkuZ 226 — 4y
0
Which means just do the .Touched function on the Server even though I don't recommend using .Touched for this. AizakkuZ 226 — 4y
0
tried to put hit.Parent:FindFirstChild("Humanoid") in the fireserver but didnt work same error WaitForChild aswell EvilTony99 -7 — 4y
Ad