Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I allow some scripts to work together?

I have made these three scripts, one randomly generates rooms that reset every 600-seconds (10 minutes), another script that displays a 600-second timer that resets once it reaches 0, and another that is in StarterGui under text label. I was wondering if it is possible to sync the timer that is seen on the player's screen with the timer that controls the resetting of the rooms. I want the timer to run out and at the same time the rooms to reset.

The script that generates rooms; (script is in SeverScriptService)

local rooms = Instance.new("Folder")

rooms.Name = "Rooms"
rooms.Parent = workspace
local timer = 600

while true do
	rooms:ClearAllChildren()
	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()
	chosenRoom.PrimaryPart = chosenPart.Floor
	chosenRoom:SetPrimaryPartCFrame(placeHolder.CFrame)

		chosenRoom.Parent = game.Workspace
	end
	wait(timer)
end

The scripts that displays the timer on the player's screen; (Script is in ServerScriptService)

local rep = game:GetService("ReplicatedStorage")
local players = game:GetService("Players")
local minutesvalue = rep:WaitForChild("Minutes")
local secondsvalue = rep:WaitForChild("Seconds")
local minutes = 10
local seconds = 0
while true do
	minutesvalue.Value = minute
	secondsvalue.Value = seconds

	repeat
		if secondsvalue.Value <=0 then
			minutesvalue.Value = minutesvalue.Value -1
			secondsvalue.Value = 59
		else
			secondsvalue.Value = secondsvalue.Value -1
		end
		wait(1)
	until secondsvalue.Valiue <=0 and minutesvalue.Value <=0

end

(Script is in StarterGui - ScreenGui - Text label. And is a localscript)

local rep = game:GetService("ReplicatedStorage")
local minutes = rep:WaitForChild("Minutes")
local seconds = rep:WaitForChild("Seconds")
local text = script.Parent

if seconds.Value <= 9 then
	text.Text = tostring(minutes.Value)..":0"..tostring(seconds.Value)
else
	text.Text = tostring(minutes.Value)..":"..tostring(seconds.Value)
end

minutes:GetPropertyChangedSignal("Value"):Connect(function()
	if seconds.Value <= 9 then
		text.Text = tostring(minutes.Value)..":0"..tostring(seconds.Value)
	else
		text.Text = tostring(minutes.Value)..":"..tostring(seconds.Value)
	end
end)

seconds:GetPropertChangedSignal("Value"):Connect(function()
	if seconds.Value <= 9 then
		text.Text = tostring(minutes.Value)..":0"..tostring(seconds.Value)
	else
		text.Text = tostring(minutes.Value)..":"..tostring(seconds.Value)
	end
end)

All scripts work on their own but are not in sync with each other. Thank you for your help!

2 answers

Easiest solution is to just send a remote event to all the players on the server when the timer commences.


local remotes = Instance.new("Folder") -- Lets go ahead and create a folder to store remotes in. You may also do these steps manually.
remotes.Name = "Remotes"
remotes.Parent = game.ReplicatedStorage
local initiatetimer = Instance.new("RemoteEvent")
initiatetimer.Name = "InitiateTimer"
initiatetimer.Parent = remotes


local rooms = Instance.new("Folder")

rooms.Name = "Rooms"
rooms.Parent = workspace

local timer = 600

while true do
	rooms:ClearAllChildren()
	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()
		chosenRoom.PrimaryPart = chosenPart.Floor
		chosenRoom:SetPrimaryPartCFrame(placeHolder.CFrame)

		chosenRoom.Parent = game.Workspace
	end
	initiatetimer:FireAllClients(timer) -- When the script starts the countdown, send a signal to all current players to start their countdown.
	wait(timer)
end

The above code goes into a script located anywhere

The below code goes into a local script located somewhere like StarterGui

--Inside localscript
local function ConvertTime(s) -- This function will return the H:M:S of any value inserted as s
	return string.format("i:i:i", s/60^2, s/60%60, s%60)
end

repeat wait() until game.ReplicatedStorage:FindFirstChild("Remotes") -- Wait for the remote to be made so it doesn't error.
local function Initiate(timer) -- Easier to make a function for the remote to access.
	for i = 1, timer do -- a for loop with a 1 second delay is the best approach
		gui.Text = ConvertTime(timer - i)-- Since timer == max number (600) you want to do timer - i, so it'll count down from 600. If you want it to count up, just use i. Using ConvertTime(timer - i) will result in the text being HMS, all with 3 lines of a function.
		wait(1)
	end
end

game.ReplicatedStorage.Remotes.InitiateTimer.OnClientEvent:Connect(Initiate) -- This line is completed, alls it does is waits for the remote to be fired, and when it does it connects that remote to the Initiate function.

This code works by sending a message to the player to start a countdown at the exact same time as the server starts it's countdown. Displaying it to the player is purely cosemetic, and should not be ran on its own countdown, as it serves no purpose but to show players when it will reset.

11 comments

0

I edited my comment to illustrate where they go better

0

Whoops just realised I forgot a script inside the text label.

0

I just update it with the script that I forgot

0

From what I can gather you just want to create a timer for 600 seconds to create the rooms, and show the player the timer, but in seconds and minutes? If so I can show you that without the need for that long third script.

0

Yeah that is exactly what I'm going for, it would be amazing if you could show me.

0

Updated the second block of code in my post that will convert the timer into HMS. Analyze the code and learn from them. Both should work as long as you place them in the right scripts.

0

and remember, you dont need to convert the timer itself, just what is shown to the player. The conversion doesn't take place until it is time to set the guis.Text

0

thanks!

0

In the first code, on line 22, the second 'chosenPart' has a blue line under it and in the localscript on line 8 'gui' has a blue line under it.

0

On line 22 you defined chosenPart. Im not sure what it was for, but you didnt include it in your post. Line 8 of the second code, define gui by doing local gui = //PathToGui

0

Looking at it, it looks like on line 22 it should be chosenRoom.PrimaryPart = chosenRoom.Floor

This discussion is preserved as it was. New answers, comments, and votes are closed.