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)
--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
-- 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)
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
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