I’m trying to make a room where the longer you’re in there, the more rats you’ll see, but the rats aren’t visible to any other players.
If there are multiple players in the room, they will have different amounts of visible rats depending on how long they’ve been in the room
I’m using a remove event that tells the client to spawn a rat but the rats do not appear
Server Script
function checkForNewPlayers()
for _,part in ipairs(partsInRegion) do
local player = game.Players:GetPlayerFromCharacter(part.Parent)
if player then
if not table.find(playerInBounds,player) then
if player.Character.Humanoid.Health > 0 then
table.insert(playerInBounds,player)
end
end
end
end
end
function removeOldPlayers()
for playerIndex,player in ipairs(playerInBounds) do
for partIndex,part in ipairs(partsInRegion) do
if player == game.Players:GetPlayerFromCharacter(part.Parent) then
if player.Character.Humanoid.Health > 0 then
break
else
table.remove(playerInBounds,playerIndex)
event:FireClient(player, "delete rats")
break
end
elseif partIndex == #partsInRegion then
table.remove(playerInBounds,playerIndex)
event:FireClient(player, "delete rats")
end
end
end
end
local spawnratcoro = coroutine.create(function()
while true do
wait(5)
for _,player in ipairs(playerInBounds) do
if player.Character.Humanoid.Health > 0 then
event:FireClient(player, "spawn a rat")
end
end
end
end)
local passivecoro = coroutine.create(function()
while true do
partsInRegion = workspace:FindPartsInRegion3WithIgnoreList(ratZone,ignoreList,math.huge)
removeOldPlayers()
checkForNewPlayers()
wait(1)
end
end)
coroutine.resume(passivecoro)
coroutine.resume(spawnratcoro)
Local Script
event.OnClientEvent:Connect(function(Type)
if Type == "spawn a rat" then
local ratclone = rat:Clone()
ratclone.HumanoidRootPart.Position = Vector3.new(math.random(xmin,xmax),-2.179,math.random(zmin,zmax))
ratclone.randommove.Enabled = true
end
end)
local spawnratcoro = coroutine.create(function()
while true do
wait(5)
for _,player in ipairs(playerInBounds) do
if player.Character.Humanoid.Health > 0 then
local ratclone = ratmodel:Clone()
event:FireClient(player, "spawn a rat", ratclone)
end
end
end
end)
client
event.OnClientEvent:Connect(function(Type, ratclone)
if Type == "spawn a rat" then
print("spawning rat")
ratclone.HumanoidRootPart.Position = Vector3.new(math.random(xmin,xmax),-2.179,math.random(zmin,zmax))
ratclone.randommove.Enabled = true
end
end)
at the moment it says “attempt to index nil with ‘HumanoidRootPart’”
event.OnClientEvent:Connect(function(Type)
if Type == "spawn a rat" then
print("spawning rat")
local ratclone = rat:Clone()
ratclone.Parent = game.Workspace
ratclone.HumanoidRootPart.Position = Vector3.new(math.random(xmin,xmax),-2.179,math.random(zmin,zmax))
ratclone.randommove.Enabled = true
end
end)
it’s spawning the rats, can’t tell if it’s only for the player or not since studio won’t connect to the server for some reason
but the rats stay above the ceiling with the initial model, and the random moving script doesn’t enable
If the rats is cloned from the client, then the scripts that moves the rats needs to be changed to a LocalScript, and then the localscript must be parented into the player PlayerScripts… or just make the rats move from the same localscript.
i want the movement of each rat to be independent of one another so should i clone localscripts of each rat and set their parent to playerscripts?
how will the localscripts know which rat they are referring to?
You can put a objectvalue in each LocalScript, and the objectvalue’s value is set to the rat… or like i said, do the movement inside the same script that spawns the npc!
here’s an example
function MovementScript(NPCModel)
local Humanoid = NPCModel.Humanoid
local Torso = NPCModel.Torso
coroutine.wrap(function()
while true do
Humanoid:MoveTo(Torso.Position + Vector3.new(math.random(-10,10),0,math.random(-10,10))
Humanoid.MoveToFinished:Wait() -- might be wrong since i forgot the function name, but it's just an example anyways!
end
end)()
end
event.OnClientEvent:Connect(function(Type)
if Type == "spawn a rat" then
print("spawning rat")
local ratclone = rat:Clone()
ratclone.Parent = game.Workspace
ratclone.HumanoidRootPart.Position = Vector3.new(math.random(xmin,xmax),-2.179,math.random(zmin,zmax))
MovementScript(ratclone)
end
end)
and that should be it! and remember, it’s just an example!
it works great! just an issue is i can’t get the rats to wait for a bit before moving again for some reason
also, when two rats collide with each other they become suspended in the air for a moment
i have collisions off for all parts of the rat but how does this still happen?
I suggest you use collisiongroups to fix the collision issues (register the collisiongroup in server since it won’t work if you do it on the client, then add the clientsided rat parts to the collisiongroup)
and for the movement cooldown, you can use task.wait() or wait(), i believe…
example
function MovementScript(NPCModel)
local Humanoid = NPCModel.Humanoid
local Torso = NPCModel.Torso
coroutine.wrap(function()
while true do
task.wait(math.random(3,6))
Humanoid:MoveTo(Torso.Position + Vector3.new(math.random(-10,10),0,math.random(-10,10))
Humanoid.MoveToFinished:Wait()
end
end)()
end
event.OnClientEvent:Connect(function(Type)
if Type == "spawn a rat" then
print("spawning rat")
local ratclone = rat:Clone()
ratclone.Parent = game.Workspace
ratclone.HumanoidRootPart.Position = Vector3.new(math.random(xmin,xmax),-2.179,math.(zmin,zmax))
MovementScript(ratclone)
end
end)