I am currently coding a power up for the player to use and the problem was that all the effects were on client side only so I had to make extensive changes so that the gear would work on the server.
The problem
My issue is how the server script is detecting the humanoid. When equipping it for the first time, the script reports that the humanoid as nil. But when I attempt to reload it, while holding the tool, it reports back as if it actually detected the humanoid and the rest of the script operates as I expect.
Code
local hnd = tool.Handle
local star = tool.Star
local activeTime = tool.timeActive
local plr = tool.Parent
local char = plr
local human = char:FindFirstChild("Humanoid") --For some reason, this is nil
local oldFlag = plr:FindFirstChild("FluukiPower")
local tmr = flk.FlkPowerTimer
function makeMePowerful()
if tool.Enabled == true and human ~= nil and oldFlag == nil then
human.WalkSpeed = 100
human.Health = 10000
human.MaxHealth = 10000
human.JumpPower = 200
end
end
tool.Enabled = true
function activate()
makeMePowerful()
end
function onEquipped()
script.Parent.PowwrStar.Standby:Play()
print(human) --Used to detect weather that "human" finds said humanoid.
end
function onUnequip()
script.Parent.PowwrStar.Standby:Pause()
end
tool.Activated:Connect(activate)
tool.Equipped:Connect(onEquipped)
tool.Unequipped:Connect(onUnequip)
Summary
Expected behavior: Have the script detect humanoid on the get go. Unintended behavior: Script reports humanoid as nil on first launch, thus not working until I reload the script while holding the tool.
Your problem is that you are trying to make a reference to the character’s humanoid before it even exists, resulting it to be nil. Simply switch local human = char:FindFirstChild("Humanoid") to local human = char:WaitForChild("Humanoid")
I have tried WaitForChild, but now it will wait forever. It will now do nothing when I equip it. My goal is to keep the code as simple as possible so an external script which simply enables this code here would be proven redundant.
Can you show what tool is defined as before I jump to conclusions on what the issue might be? If I stared at the code long enough I could figure it out myself, but if you show the hierarchy & all important variable pathways help will be easier to provide.
If the only existing problem is that the reference occurs before the Humanoid originally exists, then you can check if it exists through an if statement, and move forward depending on its existance.
Your char must not be referencing the actual character of the player. It looks like it might be referencing the backpack or wherever it’s parented to as that’s where the script will begin running in.
It would be better to define human in the OnEquipped function as it should guarantee it is in the character
First off, the script we are talking about is a ServerScript – LocalPlayer would not work in that context – and second, it would be better to use game:GetService("Players").LocalPlayer.CharacterAdded:Wait() to actially wait for the character to be added.
Using while wait() do is generally a bad practice in of itself, but there are better alternatives in this situation anyway.