Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I make this script reset itself every few minutes?

Asked by 3 years ago

I have created this script for a game, it randomly moves rooms that I have created and placed in Replicated Storage (in a folder called GeneratedRooms), to placeholder floors in the workspace (in a folder called PlaceHolders). To create a tunnel of randomly generated rooms.

The Script:

    local rooms = game.ReplicatedStorage:WaitForChild("GeneratedRooms"):GetChildren()

for I, placeHolder in pairs (game.Workspace.PlaceHolders:GetChildren()) do
    local chosenRoom = rooms[math.random(1,#room)]:Clone()
    chosenRoom.PrimaryPart = chosenRoom.Floor
    chosenRoom:SetPrimaryPartCFrame(placeHolder.CFrame)

    placeHolder:Destory()
        chosenRoom.Parent = game.Workspace
end

I was wondering how I could make this script be able to reset its self every few minutes, so it has a new set of randomly generated rooms. Currently, I have to leave and rejoin the game to get a different set of rooms.

Thanks for your help!

0
One thing you can do is have a folder in workspace called 'Rooms' or whatever youd like to name it, then parent the rooms to the folder instead of workspace. so when you want to reset the rooms, just delete everything in the folder, and repeat the process munkuush 22 — 3y

2 answers

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

Just put it in a loop that only runs every so often

--First of all you will need a way to reset the rooms that were already in place
local rooms = Instance.new("Folder") -- Create new folder to store the rooms in
rooms.Name = "Rooms"
rooms.Parent = workspace
local timer = 60 -- Change 60 to however many seconds you want to wait inbetween room changes
while true do
    rooms:ClearAllChildren() -- Removes all previous rooms
    local rooms = game.ReplicatedStorage:WaitForChild("GeneratedRooms"):GetChildren()

    for I, placeHolder in pairs (game.Workspace.PlaceHolders:GetChildren()) do
        local chosenRoom = rooms[math.random(1,#rooms)]:Clone() -- You had it set to #room? I think you meant to have it as #rooms. That just says to choose an index between 1, and the number of items in the table. 
        chosenRoom.PrimaryPart = chosenRoom.Floor
        chosenRoom:SetPrimaryPartCFrame(placeHolder.CFrame)

        placeHolder:Destory() -- Not sure about this, if you destroy the place holders you wont have any to set the next rooms at. But I have no idea what you have going on as placeholders, if they are just invisible parts just set them to cancollide false so you can keep using them
        chosenRoom.Parent = rooms
    end
    wait(timer)
end

-- You may also want to move the player so he doesnt fall to his death when old rooms are destroyed, in that case just use character.HumanoidRootPart.CFrame = SpawnPart.CFrame * CFrame.new(0,3,0) before you :ClearAllChildren()

0
Thanks, it worked perfectly. I just deleted the 'placeHolder:Destory()' part of the script and changed line 16 to say 'chosenRoom.Parent = game.Workspace' as 'chosenRoom.Parent = rooms' wasn't working. Wildspadealt 7 — 3y
0
It should have worked if you copied the whole script in. I defined rooms at the top, you could make it yourself with a folder in the workspace named rooms. I mainly did that because as you create more rooms, if you dont destroy the old ones they will glitch inside, block ones, etc. Making a folder to store them in makes it easier to :ClearAllChildren() and destroy them with 1 line WizyTheNinja 834 — 3y
Ad
Log in to vote
0
Answered by 3 years ago

You could make it so the script has a duplicated version of the script that is disabled and has a different name in lighting and you could copy it into ServerScriptService For example:

local a = lighting.DupeScript while true do wait(interval) a:Clone().Parent = workspace workspace.DupeScript.Name = "OGScript" wait(0.1) ServerScriptStorage.OGScript:Destroy() wait(0.1) workspace.OGScript.Parent = ServerScriptStorage wait(0.1) ServerScriptStorage.OGScript.Disabled = false end This is the only thing I could think of, hope it helps!

Answer this question