NPC nocliping thought client-side ground


Hello Devs mind if yall help me fix this wired thing!

from the video the ground is client-side but the npc is server-side mean that he cant see client-side but server-side then just noclip through the ground me and my friend tried fix this thing for some days but never works ( and yea the red highlight thing is the npc)

sorry i kinda bad at explaining stuff like this (also grammar) this my first time on making a real game
Thanks
[REPOST #1]

2 Likes

What did you expect?
Server doesn’t see the ground so the NPC will move through it.
What do you want to achieve by this? Why isn’t the ground on server as well?

1 Like

you should either put the ground server-side (i.e spawn it in via normal script.) or put the npc client side too.

if you could show the script and the way you spawn the ground in, i could help rescript it (or use chat gpt if you want ¯_(ツ)_/¯)

1 Like

well i have no idea how to do it

1 Like
local floor = math.floor
local abs = math.abs
local wait = wait
local ipairs = ipairs
local insert table.insert

local Chunk = require(game.ReplicatedStorage.Chunk)

local WIDTH_SCALE = Chunk.WIDTH_SCALE
local X, Z = Chunk.X, Chunk.Z
local CHUNKS_LOADED_PER_TICK = 5
local TICKS_PER_UPDATE_CYCLE = 30
local RENDER_DISTANCE = 16

local cameraChunkPosX
local cameraChunkPosZ
local loadedChunkPosX
local loadedChunkPosZ
local fastLoad = true
local chunkCount = 0
local updateCount = 0
local chunks = {}

local function getChunkPosFromCamera()
	local camPos = Vector3.new(0, 0, 0) -- replace with NPC position
	local camX = camPos.X
	local camZ = camPos.Z

	return
		floor((camX - WIDTH_SCALE) / ((X-1) * WIDTH_SCALE)),
	floor((camZ - WIDTH_SCALE) / ((Z-1) * WIDTH_SCALE))
end

local function isCamChunkPosOutOfLoadedChunkRange()
	if (loadedChunkPosX == nil or loadedChunkPosZ == nil)
		or (abs(cameraChunkPosX - loadedChunkPosX) >= RENDER_DISTANCE)
		or (abs(cameraChunkPosZ - loadedChunkPosZ) >= RENDER_DISTANCE) then
		return true
	else
		return false
	end
end

local function destroyDistantChunks()
	local n = #chunks

	for i = 1, n do
		if abs(chunks[i].x - cameraChunkPosX) > RENDER_DISTANCE
			or abs(chunks[i].z - cameraChunkPosZ) > RENDER_DISTANCE then

			chunkCount = (chunkCount + 1) % CHUNKS_LOADED_PER_TICK
			if (chunkCount == 0) and (not fastLoad) then
				wait()
			end

			chunks[i]:Destroy()
			chunks[i] = nil
		end
	end

	local j = 0
	for i = 1, n do
		if chunks[i] ~= nil then
			j = j + 1
			chunks[j] = chunks[i]
		end
	end

	for i = j + 1, n do
		chunks[i] = nil
	end
end

local function renderChunks()
	for x = cameraChunkPosX - RENDER_DISTANCE, cameraChunkPosX + RENDER_DISTANCE do
		for z = cameraChunkPosZ - RENDER_DISTANCE, cameraChunkPosZ + RENDER_DISTANCE do
			local chunkExists = false

			for index, chunk in ipairs(chunks) do
				if chunk.x == x and chunk.z == z then
					chunkExists = true
					break
				end
			end

			if not chunkExists then
				chunkCount = (chunkCount + 1) % CHUNKS_LOADED_PER_TICK
				if (chunkCount == 0) and (not fastLoad) then
					wait()
				end

				insert(chunks, Chunk.new(x, z))
			end
		end
	end
end

local function updateTerrain()
	destroyDistantChunks()
	renderChunks()

	loadedChunkPosX = cameraChunkPosX
	loadedChunkPosZ = cameraChunkPosZ
end

while true do
	cameraChunkPosX, cameraChunkPosZ = getChunkPosFromCamera()

	if isCamChunkPosOutOfLoadedChunkRange() then
		fastLoad = true
	else
		fastLoad = false
	end

	if (updateCount == 0) or (fast) then
		updateTerrain()
	end

	updateCount = (updateCount + 1) % TICKS_PER_UPDATE_CYCLE
	wait()
end

i think this is it (yea i totally stole it it is inf terrain gen)

1 Like

test it out. up at the top of studio press Current: Client and you’ll see what the server sees, if the terrian shows the npc can see it.

(also dont worry that you used freemodels / tutorials it’s best for new programmers to do.)

1 Like

client
this is client pov (a cam is forced to be first person and i lazy to disable it)
server
and server pov

Ok, so what you need to do is instead of having the terrain script client side (i.e in a local script) you need to build it server side (i.e. in “script”.) otherwise the npc wont be able to see it

my friend already do that he said “if put the script as a server-side it will not generate new chunk as it will only generate at middle”

right you have a chunk system that works server side. Im not sure if you would be able to have npc’s there, i think your best bet is to have the npcs client side (spawn them in via local script)
otherwise i have no idea

my friend solve it thank you for help

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