[help] NPC chaser goes through door

Sorry for my eventual bad english.

Questions:

  1. What do you want to achieve? Understand what is going on.

  2. What is the issue? This NPC I made, was created for chasing players. It does not normally go through walls, but when I locally (on client) set a wall’s CanCollide property to false, it is able to go through, even if on server that wall has CanCollide on true.

  3. What solutions have you tried so far? I tried messing with the script, but I really have no idea on how to solve this issue.

Chase Script

local ChaseRange = 75

local ai = script.Parent
local AIHumaniod = script.Parent.Humanoid
local AIHumaniodRootPart = script.Parent.HumanoidRootPart

local function GetClosestPlayer()
	local closestPlayer = nil
	local closestPlayerDistance = ChaseRange
	
	for i,v in pairs(game.Players:GetPlayers())do
		if v.Character then
			local charHRP = v.Character.HumanoidRootPart
			
			if (charHRP.Position-AIHumaniodRootPart.Position).Magnitude < closestPlayerDistance then -- Checks if player has a body. --
				closestPlayer = v.Character
				closestPlayerDistance = (charHRP.Position-AIHumaniodRootPart.Position).Magnitude
			end
		end
	end
		
		return closestPlayer
end

	local counter = 0

	while true do
	wait(1)
	
	
	local ClosestPlayer = GetClosestPlayer()
	if ClosestPlayer then
		AIHumaniod:MoveTo(ClosestPlayer.HumanoidRootPart.Position)
	end
	if ClosestPlayer == nil then
		counter += 1
		print(counter)
		if counter > 9 then
			AIHumaniodRootPart.CFrame = game.Workspace.coooool.CFrame
			counter = 0
		end
	end
end







for _, zambieparts in pairs(ai:GetChildren()) do
	if zambieparts:IsA'Part' then
		zambieparts.Touched:connect(function(p)
			if p.Parent.Name == GetPlayerNames() and p.Parent.Name ~= ai.Name then -- damage
				local enemy = p.Parent
				local enemyhuman = getHumanoid(enemy)
				enemyhuman:TakeDamage(999)
			end
		end)
	end
end

function GetPlayerNames()
	local players = game:GetService('Players'):GetChildren()
	local name = nil
	for _, v in pairs(players) do
		if v:IsA'Player' then
			name = tostring(v.Name)
		end
	end
	return name
end

function getHumanoid(model)
	for _, v in pairs(model:GetChildren()) do
		if v:IsA'Humanoid' then
			return v
		end
	end
end

Door Opener Script

local event = game.ReplicatedStorage.keys

wait(2)


-- Locals
local doorRed = game.Workspace:WaitForChild("Red")
local doorBlue = game.Workspace.Blue
local doorGreen = game.Workspace.Green

local buttRed = game.Workspace.Keys.KeyRed
local buttBlue = game.Workspace.Keys.KeyBlue
local buttGreen = game.Workspace.Keys.KeyGreen

local sound = script:WaitForChild("pick sound")

local soundCD = 2
local canSound = true

-- Doors settings
doorRed.Transparency = 0
doorRed.CanCollide = true

doorBlue.Transparency = 0
doorBlue.CanCollide = true

doorGreen.Transparency = 0
doorGreen.CanCollide = true


-- Keys settings
buttRed.Transparency = 0

buttBlue.Transparency = 0

buttGreen.Transparency = 0




event.OnClientEvent:Connect(function(color)
	if canSound == true then
		sound:Play()
		canSound = false
		wait(soundCD)
		canSound = true
	else
		
	end
	if color == "green" then
		doorGreen.Transparency = 0.75
		doorGreen.CanCollide = false
		
		buttGreen.Transparency = 0.75
	elseif color == "blue" then
		doorBlue.Transparency = 0.75
		doorBlue.CanCollide = false
		
		buttBlue.Transparency = 0.75
	elseif color == "red" then
		doorRed.Transparency = 0.75
		doorRed.CanCollide = false
		
		buttRed.Transparency = 0.75
	end
end)

Video demonstrating what is happening

The Door Opener Script is a LocalScript

If the NPC script is on the client as well, the door will be CanCollide false for the NPC.

No, the NPC script is a server script
Immagine 2023-08-26 220503

Verify that the RunContext is Server

Oh, it was on legacy. Thank you for responding and make me learn a new thing, I didn’t know about the existence of a “RunContext” property.

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