NPC WalkSpeed doesn't change it's value through code

Hello everybody, it’s me, again.
I use this script:

local dummy = script.Parent
local humanoid = dummy:WaitForChild("Humanoid")
while true do 
	wait(10)
	humanoid:MoveTo(Vector3.new(math.random(-20,20),0,math.random(-20,20)))
end

to make NPC wander around the place.
I want to make it stop when specifid value is being held, and i confirmed it’s effect through print()
sadly it doesnt change anything whenever i do anything
scripts I use are:

			if x and y then
				humanoid.WalkSpeed = 0
			end

what is wrong tho

to clarify every scope is placed correctly

Could you share some more of your code, so we understand what’s the x and y?

Basically i try to make sort of SCP 173 AI and code i use is checking if NPC being observed
it reads:

local _, cameraBool

while true do
	task.wait()

	local npc = workspace:FindFirstChild("NPC")
	if npc and npc:IsA("Model") then
		local npcHead = npc:FindFirstChild("Head")
		if npcHead then
			_, cameraBool = workspace.CurrentCamera:WorldToScreenPoint(npcHead.Position)
			
			local humanoid = npc:FindFirstChild("Humanoid")
			if cameraBool and humanoid.Health > 1900 then
				print(tostring(game.Players.LocalPlayer.Name) .. " can see the npc")
				humanoid.WalkSpeed = 0
			end
		end
	else
		print("NPC not found in workspace")
	end
	
end

Thing is that i receive positive feedback in output, and i worked with other scripts that i cannot copy here rn for i already deleted them after finding they’re not working. By this i mean every script i used wasn’t changing NPC’s walkspeed. Do you think it may be matter of Wandering script itself?

It seems like you are changing the WalkSpeed of the NPC on the client, which doesn’t prevent it from moving on the server, unless the Network Ownership is set to the client.

Wait i just realized what i did but wait, wait wait wait, i want to make that it doesnt move when even one player sees it. Is just setting localscript to script going to work? Sorry for messing, im scripting 1 week and everything seems little dizzy for me. I mean i put this script into startergui so it effects every player individualy

Add a RemoteEvent which will tell the server when a player is looking at the NPC or not.

Create a table on the server, containing players, which are currently looking at the NPC, and when the table becomes empty, let the NPC move again.

1 Like

Okay so the main boss of this week is going to be this, sheeesh, i love scripting, ill try my best and hopefully find some other tutorial on how to do so…

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.