Cant move if player looking at you script not working

I am currently creating a player-controlled SCP 173. The way this script is supposed to work is if you are SCP 173, you can not move if a player who is not on the SCPs team is looking at you. However, after 5 seconds of being looked at, you can move freely for a couple seconds. There is a morph allowing you to transform as the entity.

Could it be possible the script is only loading on the morph and not the player?

However, when my character changes to SCP 173 the scripts are all there
image

This is the morph explorer
image

Script

local monster = script.Parent 

local walkspeedDefault = 60
local walkspeedWhenLooked = 0
local cooldownDuration = 5  -- in seconds

local scpTeamName = "SCPs"

local function isPlayerLooking()
	for _, player in ipairs(game.Players:GetPlayers()) do
		local character = player.Character
		if character and character:IsDescendantOf(game.Workspace) then
			local humanoid = character:FindFirstChildOfClass("Humanoid")
			if humanoid and humanoid.RigType == Enum.HumanoidRigType.R6 then
				local direction = (monster.Torso.Position - character.Head.Position).unit
				local ray = Ray.new(character.Head.Position, direction * 1000)
				local part, hitPosition = game.Workspace:FindPartOnRay(ray, monster)


				if part and part:IsDescendantOf(monster) then
					return player
				end
				
				wait(0.1)
			end
		end
	end
	return nil
end

local function isPlayerInSCPteam(player)
	local team = player.Team
	if team then
		return team.Name == scpTeamName
	end
	return false
end

local function setWalkspeed(walkspeed)
	monster.Humanoid.WalkSpeed = walkspeed
end

local function onLookedAt()
	setWalkspeed(walkspeedWhenLooked)
	wait(cooldownDuration)
	setWalkspeed(walkspeedDefault)
end

while true do
	local lookingPlayer = isPlayerLooking()

	if lookingPlayer then
		if not isPlayerInSCPteam(lookingPlayer) then
			onLookedAt()
		end
	end

	wait(0.1)  -- Check for player looking every 0.1 second
end

Just a couple thoughts. At a quick glance, it looks like you’re only waiting between setting the walkspeed to 0, and then resetting it. Since you call the function every 0.1 seconds in the loop, it immediately does the check again and sets it back to 0. Thats assuming the other functions work, they look fine to me though.

First post BTW :pray:

By the way, if you wanted to check if the script is loading properly, you could probably just add print(monster) at start up and check the output (im assuming, since i havent worked with morphs yet)

I’m assuming if you want the script to only work on the NPC then you can put an if statement like this:

if game.Players:GetPlayerFromCharacter(script.Parent) then
	script.Enabled = false
end