Why wont my monster spawn?

Why wont my monster spawn? I keep getting a error on line 8
Is there anything wrong with the code or with the RunService?

I already tried changing the RunService to while true do

How can I make it so that the monster will have a chance to spawn every second?

If this is code on the server, i dont think you can use RenderStepped on the server.

Youve tried while true do but have you tried while task.wait(1) do

Try this.

local RunService = game:GetService("RunService")

RunService.RenderStepped:Connect(function()
	--code here
end)

Where is this Script Located?

If its a LocalScript, use a Server Script
if a Server Script, should work for every player

If its inside ServerScriptService, just put the Monster Inside the script

while task.wait(MDT) do
CD = true
local RM = math.random(1,10)

if RM == 1 then
CD = false
local Mob = script.Monster:Clone()
Mob.Parent = workspace
Mob:SetPrimaryPartCFrame(Spawn1.CFrame + Vector3.new(0,2,0))
elseif RM == 2 then
CD = false
local Mob = script.Monster:Clone()
Mob.Parent = workspace
Mob:SetPrimaryPartCFrame(Spawn2.CFrame + Vector3.new(0,2,0))
elseif RM == 3 then
CD = false
local Mob = script.Monster:Clone()
Mob.Parent = workspace
Mob:SetPrimaryPartCFrame(Spawn3.CFrame + Vector3.new(0,2,0))
elseif RM == 4 then
CD = false
local Mob = script.Monster:Clone()
Mob.Parent = workspace
Mob:SetPrimaryPartCFrame(Spawn4.CFrame + Vector3.new(0,2,0))
end

end


You cant, You can only use Stepped and Heartbeat on the Server

@alexjouq This does the same same thing, there is not really a point in doing it


I would recommend just numbering the Monsters to randomize which Monster will be cloned

First, here’s the code in text form using OCR, but you should really post your code in the thread:

cd = false
local Spawn1 = game.Workspace["Roomate/CheackMateSpawnPoints"].UpperSpawnPoint
local Spawn2 = game.Workspace["Roomate/CheackMateSpawnPoints"].LowerSpawnPoint
local Spawn3 = game.Workspace["Roomate/CheackMateSpawnPoints"].RightSpawnPoint
local Spawn4 = game.Workspace["Roomate/CheackMateSpawnPoints"].LeftSpawnPoint
local MDT = 5

game["Run Service"].RenderStepped:Connect(
    function()
        if cd == false then
            cd = true
            local RandomNumber = math.random(1, 10)
            if RandomNumber == 10 then
                local RandomNumber2 = math.random(1, 4)
                if RandomNumber2 == 1 then
                    local Monster = game.ServerStorage.Monsters.Roomate
                    local MonsterClone = Monster:Clone()
                    MonsterClone.Parent = game.Workspace
                    MonsterClone.HumanoidRootPart.CFrame = Spawnl.CFrame
                    wait(MDT)
                    cd = false
                end
                if RandomNumber2 == 2 then
                    local Monster = game.ServerStorage.Monsters.Roomate
                    local MonsterClone = Monster:Clone()
                    MonsterClone.Parent = game.Workspace
                    MonsterClone.HumanoidRootPart.CFrame = Spawn2.CFrame
                    wait(MDT)
                    cd = false
                end
                if RandomNumber2 == 3 then
                    local Monster = game.ServerStorage.Monsters.Roomate
                    local MonsterClone = Monster:Clone()
                    MonsterClone.Parent = game.Workspace
                    MonsterClone.HumanoidRootPart.CFrame = Spawn2.CFrame
                    wait(MDT)
                    cd = false
                end
                if RandomNumber2 == 4 then
                    local Monster = game.ServerStorage.Monsters.Roomate
                    local MonsterClone = Monster:Clone()
                    MonsterClone.Parent = game.Workspace
                    MonsterClone.HumanoidRootPart.CFrame = Spawn4.CFrame
                    wait(MDT)
                    cd = false
                end
            end
        end
    end
)

Second, here’s the code but vastly less redundant:

local spawnPointContainer = workspace:FindFirstChild("Roomate/CheackMateSpawnPoints")
local spawnPoints = {}
for _, dir in {"Upper", "Lower", "Right", "Left"} do
    table.insert(spawnPoints, spawnPointContainer:FindFirstChild(dir .. "SpawnPoint"))
end
local MDT = 5

while true do
    task.wait(1)
    local RandomNumber = math.random(1, 10)
    if RandomNumber == 10 then
        local Monster = game.ServerStorage.Monsters.Roomate
        local MonsterClone = Monster:Clone()
        MonsterClone.Parent = workspace
        local SpawnCFrame = spawnPoints[math.random(1, #spawnPoints)].CFrame
        MonsterClone.HumanoidRootPart.CFrame = SpawnCFrame
        task.wait(MDT)
    end
end

Finally, post errors that you see from the Output console in this thread.

1 Like

This is cool, but it does the same thing either way.

Not true. To add a spawn point container in the new code, all he has to do is add the first part of the container name here:

Whereas before, he would’ve had to copy paste another set of code blocks. I know OP is new to programming, so I figured I’d show a side by side of copy pasted code and efficient code.

Looking at it, yes it does do the same thing, its just made to look more complicated

The scirpt is located in WorkSpace. Does this effect anything???
Screenshot 2022-11-15 161752

Each monster has its own script. Some have a chance to spawn every second, Some will spawn every Minute. Some will spawn once every day passes in my game

Put all the monster spawning logic in one script and put that script in ServerScriptService.

Put the monsters in a folder in ReplicatedStorage and clone them into the workspace.

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