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
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!
while RunService.Stepped:Wait() do
local Distance = (Slime.HumanoidRootPart.Position - PlayerRootPart.Position).Magnitude
if Distance > RenderingRange then
Slime.Parent = game.ReplicatedStorage.DespawnedSlimes
end
end