NPC attracted to light

Hi everyone! I am trying to make an NPC attracted to light. Let me show the script so I may explain.

game.Players.PlayerAdded:Connect(function(player)
	local humanoidnpc = script.Parent.Humanoid
	
	player.CharacterAdded:Wait()
	local Torso = player.Character.UpperTorso
	
	if game.ReplicatedStorage.Value.Value == true then
		humanoidnpc:MoveTo(Torso.Position)
	end
end)

Basically, what I am trying to accomplish is when the boolean value is activated (scripted in a separate flashlight script), i want the npc to move to the players torso’s position. Unfortunately this script doesn’t work. What should I do?

1 Like

Wait so just to clarify, you want

humanoidnpc:MoveTo(Torso.Position)

to run whenever

game.ReplicatedStorage.Value.Value

is changed to true?

Yes. That’s what I was trying to do in the script.

Instead of what you posted, try this:

game.Players.PlayerAdded:Connect(function(player)
	local humanoidnpc = script.Parent.Humanoid
	
	player.CharacterAdded:Wait()
	local Torso = player.Character.UpperTorso

    -- Connecting to whenever your value is changed
    game.ReplicatedStorage.Value.Changed:Connect(function(newValue)
        -- Check if new value is true
        if newValue == true
            -- If new value is true, execute the code (moving the npc)
            humanoidnpc:MoveTo(Torso.Position)
        end
    end)
end)

Thanks for this. It works very well, however the npc only goes to the last known location of the UpperTorso. Like if you move when you toggle the light and the value becomes true, the npc only goes to where your torso’s position was when you first activated it. How can I fix this?

Well that would be because it’s only called once, which is when the value is changed. What I recommend is connect to yet another function, specifically the

Humanoid.Running

function.

Here is an example of how your full code should look like:

game.Players.PlayerAdded:Connect(function(player)
	local humanoidnpc = script.Parent.Humanoid
	
	player.CharacterAdded:Wait()
	local TorsoPosition = player.Character.UpperTorso.Position

    -- Connecting to whenever your value is changed
    game.ReplicatedStorage.Value.Changed:Connect(function(newValue)
        -- Check if new value is true
        if newValue == true then
            -- If new value is true, execute the code (moving the npc)
            humanoidnpc:MoveTo(TorsoPosition)

            player.Character.Humanoid.Running:Connect(function()
                if game.ReplicatedStorage.Value.Value == true then
                    local lastPos
	                if not lastPos then
		                lastPos = player.Character.UpperTorso.Position
	                end

	                if (player.Character.UpperTorso.Position - lastPos).Magnitude > 0.2 then
		                humanoidnpc:MoveTo(player.Character.UpperTorso.Position)
	                end
                end
             end)
        end
    end)
end)

Note: This is not optimized, nor tested, if you need help optimizing it, then ask, and if it doesn’t work, I will see what I can do to fix it.

Hope this helps!

2 Likes

There is a second parameter that can be passed to MoveTo which is the part to move to. Use that second parameter, and as the part moves, the MoveTo will update its target. Note that MoveTo only moves a humanoid for 8 seconds and then stops. You have to keep calling it if it doesn’t hit its target by then.

1 Like

Thanks for the reply. I tested it, but it only goes now to the first ever UpperTorso position when the value is activated and set to true. Again I appreciate your response.

I was thinking of making a function be called when it touches a player, but I am not sure if it will work