My Random Respawn System isnt Respawning

I want to make it so when a Rock is broken it shoots out a Remote Event which them makes a respawn timer and then clone another part to a random position again

The part randomly spawns in for the first time but after it is destroyed the part no longer respawns

I looked at a few Forums explaining randomization and when I implemented it it didnt workout

--Services
local RS = game:GetService("ReplicatedStorage")
local SG = game:GetService("StarterGui")
local WS = game:GetService("Workspace")
local UIS = game:GetService("UserInputService")

--Local Vars
local Rock = RS.Rocks.Rock2
local Event = RS.Remotes.RockDestory
local RespawnTime = 3


local possibleLocations = WS.RockPositions:GetChildren()


--Rock Spawning For first Time
local clone = Rock:Clone()
local RandomPos = possibleLocations[math.random(1, #possibleLocations)]
clone.Position = RandomPos.Position
clone.Parent = WS.Rocks


--Rock Respawn
Event.OnServerEvent:Connect(function()
	task.wait(RespawnTime)
	clone:Clone()
	clone.Position = RandomPos.Position
end)

If you need anymore info feel free to ask

1 Like
--Services
local RS = game:GetService("ReplicatedStorage")
local SG = game:GetService("StarterGui")
local WS = game:GetService("Workspace")
local UIS = game:GetService("UserInputService")

--Local Vars
local Rock = RS.Rocks.Rock2
local Event = RS.Remotes.RockDestory
local RespawnTime = 3


local possibleLocations = WS.RockPositions:GetChildren()


--Rock Spawning For first Time
local clone = Rock:Clone()
local RandomPos = possibleLocations[math.random(1, #possibleLocations)]
clone.Position = RandomPos.Position
clone.Parent = WS.Rocks


--Rock Respawn
Event.OnServerEvent:Connect(function()
	task.wait(RespawnTime)
	local NewRandomPos = possibleLocations[math.random(1, #possibleLocations)]
	local NewRock = clone:Clone()
	NewRock.Position = NewRandomPos.Position
end)



--[[
   Changed:
   * The random position will now generate when called, not just one single time.
   * clone:Clone() clone.Position = RandomPos.Position (Here, you should've used a local to identify the new clone, not doing this will set the already cloned rock's position to RandomPos)
]]

I have fixed your code. Read the notes to see what problems occurred and let me know if i missed anything

I read the code and understood what you did however the respawn still doesnt work im not sure why

Try looking at console output or printing it.

1 Like

seems like the Remote Event isnt even being recived

--Rock Respawn
Event.OnServerEvent:Connect(function()
	print"recived"
	task.wait(RespawnTime)
	print("Waited")
	local NewRandomPos = possibleLocations[math.random(1, #possibleLocations)]
	local NewRock = clone:Clone()
	NewRock.Position = NewRandomPos.Position
	print("Done")
end)


None of these are printing

-- Services
local RS = game:GetService("ReplicatedStorage")
local WS = game:GetService("Workspace")

-- Variables
local Rock = RS.Rocks.Rock2
local Event = RS.Remotes.RockDestroy
local RespawnTime = 3

local possibleLocations = WS.RockPositions:GetChildren()

-- Function to spawn a rock at a random location
local function spawnRock()
	local clone = Rock:Clone()
	local randomPos = possibleLocations[math.random(1, #possibleLocations)]
	clone.Position = randomPos.Position
	clone.Parent = WS.Rocks
end

-- Initial Rock Spawn
spawnRock()

-- Rock Respawn on Event Trigger
Event.OnServerEvent:Connect(function()
	task.wait(RespawnTime)
	spawnRock()
end)

I see what you did but with this the rock doesn’t even spawn I checked the output and it gave me this:

RockDestroy is not a valid member of Folder “ReplicatedStorage.Remotes”

Set the clone’s parent to workspace or WS.Rocks

Its already set to that in the local dunction to spaen the rock

-- Function to spawn a rock at a random location
local function spawnRock()
	local clone = Rock:Clone()
	local randomPos = possibleLocations[math.random(1, #possibleLocations)]
	clone.Position = randomPos.Position
	clone.Parent = WS.Rocks --------right here
end

That is because the event is named RockDestory not RockDestroy. Just rename that.

are you willing to share the place file? if not i understand. would make it a lot easier to see the other script and set up

I dont mind

MiningTest.rbxl (63.9 KB)

sorry for the delay. gonna look at it now

1 Like

I have also implemented a random chance of getting a different type of rock such as coal to spawn in the spawn locations so here is my new script:

-- Services
local RS = game:GetService("ReplicatedStorage")
local WS = game:GetService("Workspace")

-- Variables
local Rock = RS.Rocks.Rock2
local Event = RS.Remotes.RockDestory
local RespawnTime = 3

local possibleLocations = WS.RockPositions:GetChildren()

-- Function to spawn a rock at a random location
local function spawnRock()
	local RandomRock = RS.Rocks:GetChildren() -- Get all rocks in the Rocks folder
	local ChooseRock = RandomRock[math.random(1, #RandomRock)] -- Select a random rock
	
	print(ChooseRock)
	print("spawning") --Test Prints
	
	local clone = ChooseRock:Clone() -- Clones Our Random Rock
	local randomPos = possibleLocations[math.random(1, #possibleLocations)] -- Places Rock in one of the Locations
	randomPos.IsTaken.Value = true
	

	clone.Position = randomPos.Position
	clone.Parent = WS.Rocks
end

-- Initial Rock Spawn
spawnRock()

-- Rock Respawn on Event Trigger
Event.OnServerEvent:Connect(function()
        print("recived") --test print
	task.wait(RespawnTime)
	spawnRock()
end)

it spawns with a Random Chance of location and rock just fine but when the rock is detroied it still doesnt respawn and even with the test print at the beggining of the Remote Event.OnclientEvent I dont get a “recived” print in the output anything helps thank you

I solved it with making it local and changing the FireClient to FireServer: