Render Distance For Npc's Like Minecraft

im Creating A Game Like Slime Rancher But The Game Performance Is Bad Because There Are A Lot Of Slimes, I Tried To Fix It And It Doesn’t Work, It Despawns Npc’s But Doesn’t Render Them Back, Here’s What Iv’e Done So Far:

Server:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

local Slimes = ReplicatedStorage.Slimes

local RenderingRange = 100

function SpawnSlime(spawnPart, Type, PlayerRootPart)
	local Slime = Slimes:FindFirstChild(Type)
	
	if Slime then
		Slime = Slime:Clone()
		Slime.Parent = workspace.GeneratedSlimes
		Slime:MoveTo(spawnPart.Position + Vector3.new(math.random(-10,10), 0, math.random(-10,10)))
			Slime.Parent = workspace.GeneratedSlimes
		
		if Type == "CommonSlime" then
			Slime.Head.Color= Color3.new(math.random(0,255), math.random(0,255), math.random(0,255))
		end
		
		while wait() do
			local Distance = (Slime.HumanoidRootPart.Position - PlayerRootPart.Position).Magnitude
			if Distance > RenderingRange then
				Slime.Parent = game.ReplicatedStorage.DespawnedSlimes
			end
		end
	else
		warn("The Slime Named:", Type, "DoesNotExist")
	end
end

Players.PlayerAdded:Connect(function(player)
	local Character = player.Character or player.CharacterAdded:Wait()
	if Character then
		local PlayerRootPart = Character:WaitForChild("HumanoidRootPart")
		for index, spawnpart in pairs(workspace.SlimeSpawn:GetChildren()) do
			spawnpart.Transparency = 1
			local random = math.random(10,15)
			spawnpart:SetAttribute("Amount", random)
			spawnpart:SetAttribute("MaxRandom", random)
			for i=7, random do
				coroutine.wrap(SpawnSlime)(spawnpart, "CommonSlime", PlayerRootPart)
			end
		end
	end
end)

I Know I Done Something Wrong But I Dont Know What Is It :thinking:

2 Likes

There is no mechanism to bring the slime back into workspace.GeneratedSlimes when the player comes within range again

I Think I’ve Got It!
Im Gonna Create An Event That Will Wait Until The Player Returns In Range

And I Think It Doesn’t Work Because This:

function SpawnSlime(spawnPart, Type, PlayerRootPart)
	local Slime = Slimes:FindFirstChild(Type)
	
	if Slime then
		Slime = Slime:Clone()
		Slime.Parent = workspace.GeneratedSlimes
		Slime:MoveTo(spawnPart.Position + Vector3.new(math.random(-10,10), 0, math.random(-10,10)))
			Slime.Parent = workspace.GeneratedSlimes
		
		if Type == "CommonSlime" then
			Slime.Head.Color= Color3.new(math.random(0,255), math.random(0,255), math.random(0,255))
		end
		
		while wait() do
			local Distance = (Slime.HumanoidRootPart.Position - PlayerRootPart.Position).Magnitude
			if Distance > RenderingRange then
				Slime.Parent = game.ReplicatedStorage.DespawnedSlimes
			end
		end
	else
		warn("The Slime Named:", Type, "DoesNotExist")
	end
end

This Function When The Slime Gets Cloned All This Chunk Of Code Goes To The Slime, So If I Return The Event Is In The Server And Not In The Slime Anymore!

Yes, that is correct. You’ll need a system in the main script to periodically check for slimes that become in range again and render them back in.

You could also just enable StreamingEnabled, and remove the load in and out code

What Is StreamingEnabled?
Because I Don’t Know All Of The Lua Coding Language ._.

Change wait to game[“Run Service”].Stepped:Wait()

you mean this: wait()?
if it This ill’Try

1 Like

Yes that it’ll scale with game performance and it wont be laggy. Its good for things that constantly run

1 Like

oh wait.
i think im an idiot,

		while RunService.Stepped:Wait() do
			local Distance = (Slime.HumanoidRootPart.Position - PlayerRootPart.Position).Magnitude
			if Distance > RenderingRange then
				Slime.Parent = game.ReplicatedStorage.DespawnedSlimes
			end
		end

i didn’t add an else ._.

I WAS TRUE i Didn’t Add An Else In The If Statement

Sorry for the late response, but here’s a documentation article about Streaming: Instance Streaming | Documentation - Roblox Creator Hub

oh and it was enabled automatically

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