I want the Rig called “Rat” to move to a random player, deal damage to the player and play the animation.
What is the issue? Keep it simple and clear!
Argument 1 missing or nil.
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I’ve looked everywhere but haven’t gotten any luck.
Any tips, hints or help would be very appreciated!
(The script runs on the Server, Legacy RunContext, the script starts disabled, then after I load, I enable it)
local figure = script.Parent
local Debris = game:GetService("Debris")
local deb = false
wait()
function getHumanoid(model:Model)
for i,v in ipairs(model:GetChildren()) do
if v:IsA("Humanoid") then
return v
end
end
end
function getRootPart(model:Model)
for i,v in ipairs(model:GetChildren()) do
if v.Name == ("HumanoidRootPart" or "RootPart" or "HumanoidRoot" or "HumanRootPart" or "HRootPart" or "HRoot" or "HrootPart" or "humanoidrootpart" or "Humanoidrootpart") then
return v
end
end
end
local humanoid = getHumanoid(figure)
local rootpart = getRootPart(figure)
local Attack = humanoid:LoadAnimation(figure.Attack)
humanoid.WalkSpeed=script.Parent:GetAttribute("WalkSpeed")
Attack.Priority = Enum.AnimationPriority.Action2
Attack.Looped = false
local function getRandomPLRCharacter()
local players = game.Players:GetPlayers()
return players[math.random(1,#players)].Character
end
local function attack(target)
local distance = (rootpart.Position - getRootPart(target).Position).Magnitude
local Targethumanoid = getHumanoid(target) or target.Humanoid or target:FindFirstChildWhichIsA("Humanoid") or target:WaitForChild("Humanoid")
if distance > 2 then -- This is the distance of studs the Smiler can hit you at.
humanoid:MoveTo(getRootPart(target).Position)
else
if not target:FindFirstChild("ForceField") and not deb then
deb = true
Targethumanoid:TakeDamage(script.Parent:GetAttribute("Damage")) --line 43
local s1 = figure.Head.Folder:GetChildren()
s1[math.random(1,#s1)]:Play()
Attack:Play()
local ff = Instance.new("ForceField",target)
game:GetService("Debris"):AddItem(ff,(Attack.Length + 0.2))
deb = false
end
end
end
local ChosenTarget = getRandomPLRCharacter()
while true do
task.wait(0.2)
attack(ChosenTarget) --line 58
end
Make sure you set the attribute for the figure (script.Parent) on the server or before playing the game as client changes don’t replicate to a server script
First off, make sure you are doing the “:GetAttribute(“Damage”)” at the right child (it’s currently script.Parent). If that doesn’t fix it, then you might have forgotten to set the “Damage” attribute to something.
Also, suggestion! Instead of having a function “getHumanoid”, with it running though all children of ‘model’, you can just do “model:FindFirstChild(“Humanoid”)”, which will be nil if it doesn’t exist! Same goes for “getRootPart”. Just do “model:FindFirstChild(“HumanoidRootPart”)”
About the “getHumanoid” function. It’s just not necessary to loop through all children of “model”. Just using “mode:FindFirstChild(“Humanoid”)” is faster and shorter!
About the “getRootPart” function. You did 'v.Name == (“HumanoidRootPart” or “RootPart” or “HumanoidRoot” or … etc) is a bit messy, but understandable.
What it does, is checks if “HumanoidRootPart” string is NOT false/nil, which it isn’t, so THEN it’s being compared to v.Name.
If the “HumanoidRootPart” string is false/nil, then it will go to the next string, and repeat the cycle until all strings were checked.
Might have explained weirdly, but what your code does is just basically checking if ‘v.Name == “HumanoidRootPart”’, and doesn’t check the rest.
You can do the same thing as for the “getHumanoid” function. Just by doing “model:FindFirstChild(“HumanoidRootPart”)”
Edit:
Simplification for the last part. It finds the first valid element in the parentheses and then compares that, so (“HumanoidRootPart” or “RootPart” or “HumanoidRoot” or etc…) returns just “HumanoidRootPart”.
Simplifying the equation returns just ‘v.Name == “HumanoidRootPart”’.
To fix that, just compare them individually, or maybe have a table of valid elements and check if table.find(v.Name {“HumanoidRootPart”; etc}) ~= nil