Scripts won't work

Hello.

I’ve been trying to make a mob system using simple scripts inside of mob model and when I run the game those scripts won’t actually work for some reason… When I run my game, Animation script won’t load any of animations or play it. My mob is just sitting there and doing nothing. He is not anchored. He only got welds inside of UpperTorso, because I want to weld eyes and other stuff instead of making joints. If I click on the select tool in Studio and drag around my mob, idle animations starts working and my mob is following me, dealing damage – It’s working actually, but after 5-10 seconds it stops again and if I drag him around he won’t play animation, follow me or deal some damage…
Here is the picture of my mob’s parts.


Thanks.

It might be helpful to actually show the source of the scripts that you’re having issues with, as we don’t have too much information to go off right now.

3 Likes

–MobConfig Script

local module = {
   
    --] Humanoid
    MobHealth = 50,
    MobWalkSpeed = 14,
    --] Damage
    MobDamage = 10,
    --] Respawn
    RespawnTime = 5,
    --] Follow
    FollowDistance = 20,
}
return module

–Respawn Script

local _M = require(script.Parent.MobConfig)
local Mob = script.Parent
local Enemy = Mob.Enemy
 
local Clone = Mob:Clone()
 
function Respawn()
    if (not Mob) then return end
    script.Parent:Destroy()
    wait(_M.RespawnTime)
    Clone.Parent = game.Workspace:WaitForChild("MobHolder")
    Clone:MakeJoints()
end
 
Enemy.Died:Connect(Respawn)

–MobDamageScript

local _M = require(script.Parent.MobConfig)
local Mob = script.Parent
local Enemy = Mob.Enemy
 
local DamageMiddle = false
 
function Damage(hit, DamageCoolDown)
    if game.Players:GetPlayerFromCharacter(hit.Parent) then
        local player = game.Players:GetPlayerFromCharacter(hit.Parent)
        if (not player.Character) then return end
        local character = player.Character
        if (not character:FindFirstChild("Humanoid")) then return end
        local humanoid = character.Humanoid
        humanoid:TakeDamage(_M.MobDamage)
    end
end
 
Mob.HitBox.Touched:Connect(function(hit)
    if (Enemy.Health < 1) then return end
    if (not DamageMiddle) then
        DamageMiddle = true
        Damage(hit, DamageMiddle)
        wait(1)
        DamageMiddle = false
    end
end)

–Follow Script

local _M = require(script.Parent.MobConfig)
local Mob = script.Parent
local Enemy = Mob.Enemy
 
function findNearestTorso(pos)
    local list = game.Workspace:children()
    local torso = nil
    local dist = _M.FollowDistance
    local temp = nil
    local human = nil
    local temp2 = nil
    for x = 1, #list do
        temp2 = list[x]
        if (temp2.className == "Model") and (temp2 ~= script.Parent) then
            temp = temp2:findFirstChild("HumanoidRootPart")
            human = temp2:findFirstChild("Humanoid")
            if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
                if (temp.Position - pos).magnitude < dist then
                    torso = temp
                    dist = (temp.Position - pos).magnitude
                end
            end
        end
    end
    return torso
end
 
while true do
    wait(0.1)
    local target = findNearestTorso(script.Parent.HumanoidRootPart.Position)
    if target ~= nil then
        Enemy:MoveTo(target.Position, target)
    end
end

–Humanoid Script

local _M = require(script.Parent.MobConfig)
local Mob = script.Parent
local Enemy = Mob.Enemy
Enemy.MaxHealth = _M.MobHealth
Enemy.Health = Enemy.MaxHealth
Enemy.WalkSpeed = _M.MobWalkSpeed

I recommend replacing those welds with Motor6D. They are the same thing but welds cannot be animated I believe.

Scripting questions should be posted to the appropriate subcategory, #development-support:scripting-support.


Check your assembly, surface types, joint types, anchored states, code and console output when running the scripts. Your scripts may not even be the issue, it may be your model. If you’re sure that your model is fine and you’re able to confirm that, then taking a look at the code is the next step.

You may even have to look over the animation itself.

I am using welds to weld eyes to the upper torso. It means like you are animating upper torso WITH these eyes together. Using Motor6D’s will cause a lag.

If I change the name “Enemy” to “Humanoid” then in the Follow Script, mob might get confused.

I fixed it! The problem was the HitBox’s size. It needs to be same as HumanoidRootPart is. Thanks!

1 Like

It’s weird that it works when you drag it around in Studio, then stops again. Have you ruled out the possibility that it’s a Studio bug? Try uploading the place to Roblox and joining it.