For some reason, I can’t find the humanoid for my tool…I searched up, but the ways I looked at didn’t work for some reason.
The output says: Players.SilentSuprion.Backpack.Sword.Script:8: attempt to index nil with 'Character’ Script:
local Player = game.Players.LocalPlayer
local Humanoid = Player.Character:WaitForChild("Humanoid")
Also this is a server script so it might because of that I’m not sure.
Thank you for reading
It doesn’t look like you have provided the entire script. The only possibility is that the Player.Character is somehow nil because replication order is completely out of line or not as predicted. Why do you need the Humanoid in this case?
There’s probably still mistakes since I’m still creating it but, here
local Sword = script.Parent.Parent:WaitForChild("Sword")
local Handle = script.Parent:WaitForChild("Handle")
local Debounce = 0
local Damage = 10
local Player = game.Players.LocalPlayer
local Humanoid = Player.Character:WaitForChild("Humanoid")
Handle.Touched:Connect(function()
if Debounce == 0 then
Humanoid:TakeDamage(Damage)
Debounce = 1
wait(1)
Debounce = 0
end
end)
You said this is in a server script? You can’t use game.Players.LocalPlayer in a server script, hence the name. You’ll need a different reference to the player (eg. by using the parameter provided in the Touched event).
This won’t work in a localscript, damage won’t replicate properly and will cause issues with other players in the game (others won’t be able to see that this person took damage), and if the part kills this player, the player won’t die to others.
The .Touched event provides the part that the part collided with. If it hit a player, the part will be part of the player’s character, right?
Handle.Touched:Connect(function(hit)
local character = hit.Parent -- eg. if the sword hits their arm, their arm is in their character
local player = game.Players:GetPlayerFromCharacter(character) -- yes, this method exists
if player ~= nil then
-- process
end
end)
You can go as far as skipping the player check, and use a Humanoid check instead for your case (the sword will be able to damage anything with a humanoid, including NPCs).
local humanoid = character:FindFirstChild("Humanoid") -- do not use a WaitForChild, your code will pause forever if it doesn't exist, plus there's no reason to wait (it will exist or it won't).
if humanoid ~= nil then
-- process
end
You can find the humanoid by using a argument (hit). So basically it would be like
``
local Player = game.Players
Handle.Touched:Connect(function(hit)
local Humanoid = hit.Parent.Humanoid
if Debounce == 0 then
Humanoid:TakeDamage(Damage)
Debounce = 1
wait(1)
Debounce = 0
end
end)``
Woops! Looks like the sword you are using is going to deal backfiring damage. However, you will still need your own character to avoid accidental collisions between your sword and the character. With this information, use an if statement to filter out the touched event hitting your own character. If the sword hits a random BasePart, check for the first ancestor which is a model(use FindFirstAncestorOfClass) and then find the humanoid within it. If it exist, deal damage.