Hello everyone!
So what I want to achieve are random events (scripts) that are activated over time.
I mean that every X minutes, an event (power blackout, broken pipes, flood) occurs. But I’ve no clue how to do this.
3 Likes
All help is appreciated. Thanks in advance.
local eventsTable = {
"Blackout" -- add a comma for more
}
math.random(#eventsTable)
Here’s how my code would go:
wait(math.random(X, Y))
event()
In order to do this you need a list of events (and the corresponding function that makes them run) and a loop that loops every X minutes.
This can be accomplished with RunService
and a table
-- get RunService
local RunService = game:GetService("RunService")
-- the table of events
-- to add another event, add another table into the list with name and f
local events = {
{
name = "Power Blackout",
f = function()
-- code that make the blackout happen
end
},
{
name = "Flood",
f = function()
-- code that make the flood happen
end
}
-- etc
}
local INTERVAL_TIME = 5 * 60 -- this is how long between each event in seconds, so 5 minutes in this case
local timer = 0 -- this will act as our timer, duh 😜
-- this is an event that fires every roblox heartbeat (~30 times per second)
-- it provides the argument dt, which is the amount of time in seconds since the last heartbeat
RunService.Heartbeat:Connect(function(dt)
timer += dt -- increase the timer
-- if the timer is greater than the INTERVAL_TIME, then its time to do an event
if timer > INTERVAL_TIME then
timer = timer % INTERVAL_TIME -- Reset the timer
-- pick a random event
local event_index = math.random(#events)
-- the event, you can do whatever you need to do with these values
local event = events[event_index]
local event_name = event.name
local event_f = event.f
-- run the funciton and print which event happened
print("There is a " .. event_name)
event_f()
end
end)
If you have any other question about the code, feel free to ask.
5 Likes
local Game = game
local Workspace = workspace
local Players = Game:GetService("Players")
local RandomObject = Random.new()
local EventNames = {"SlowSpeed", "FastSpeed", "LowGravity", "HighGravity", "LowJump", "HighJump"}
local Events = {
SlowSpeed = function()
for _, Player in ipairs(Players:GetPlayers()) do
local Character = Player.Character
if not Character then continue end
local Humanoid = Character:FindFirstChildOfClass("Humanoid")
if not Humanoid then continue end
Humanoid.WalkSpeed = 8 --Half of default.
end
end,
FastSpeed = function()
for _, Player in ipairs(Players:GetPlayers()) do
local Character = Player.Character
if not Character then continue end
local Humanoid = Character:FindFirstChildOfClass("Humanoid")
if not Humanoid then continue end
Humanoid.WalkSpeed = 32 --Double default.
end
end,
LowGravity = function()
Workspace.Gravity = 98.1 --Half of default.
end,
HighGravity = function()
Workspace.Gravity = 392.4 --Double default.
end,
LowJump = function()
for _, Player in ipairs(Players:GetPlayers()) do
local Character = Player.Character
if not Character then continue end
local Humanoid = Character:FindFirstChildOfClass("Humanoid")
if not Humanoid then continue end
Humanoid.JumpPower = 3.6 --Half of default.
end
end,
HighJump = function()
for _, Player in ipairs(Players:GetPlayers()) do
local Character = Player.Character
if not Character then continue end
local Humanoid = Character:FindFirstChildOfClass("Humanoid")
if not Humanoid then continue end
Humanoid.JumpPower = 14.4 --Double default.
end
end,
}
while true do
local EventName = EventNames[RandomObject:NextInteger(1, #EventNames)]
Events[EventName]()
task.wait(20) --Wait 20 seconds between events.
end
I got side-tracked but I wrote this 10-15 minutes ago. I provided some example events that you can take inspiration from.
3 Likes