How to make a random event fire

  1. What do you want to achieve?
    I want to create a script that chooses a random event every 15 seconds to happen.

  2. What is the issue?
    Since I am not really great at scripting, I’m not really sure how to create the script.

  3. What solutions have you tried so far?
    I’ve tried using math.random and then get a number from it and assign the numbers with events, but that clearly didn’t work.

I am not asking for full scripts, I’m just asking for some possible things I didn’t know before or just tips.
I’m beginner to scripting, so I am not that experienced! Any help/tips are appreciated!

5 Likes

math.random might not always return numbers you want(for example, math.random(1, 2) can return a decimal). A solution to this is using math.floor(), which returns the largest integer less than or equal to than the number inside.
I would recommend using math.floor(math.random(startNumber, endnumber) + 0.5)
This rounds the number to an integer.

I don’t actually believe that’s true.

using

math.random(1, 10)

returns an integer in the range 1 to 10.

3 Likes

Well-

You could try putting all of the events in a table such as:

local events = {event1, event2, event3, event4}

Then you can use math.random for it to select one.

E.g:

local chosenevent = events[math.random(1,4)] --change 4 to however many events there are

print(chosenevent)

Of course, you will have to have the events scripted. But see if this can point you in the right direction.

3 Likes

What i would do is to place stuff in a folder, like a folder in the game.ServerStorage and then i have the script to choose a random item from that folder to do a random event. It can be easy to configure as you can add stuff into the folder or remove stuff from the folder without having to edit the script in charge.

This works well

But I would do

local chosenevent = events[math.random(1,#events)] -- if there are 4 things in the table, #events = 4

because if you add anything else to the table, you would have to change the number in math.random as well

2 Likes

I do something similar in my game when playing random music to all players.

local events = eventsFolder:GetChildren()

--// Loop and fire a random event every loop
while true do
    local randomEvent = events[math.random(1, #events)] --> Get random event from the folder of events.
    randomEvent:FireAllClients() --> Just an example, I don't know if your situation is client or server but you get the idea fire the random event.
    wait(15) --> Wait before looping and repeating the process. 
end

Thanks, seems logical to me, but I get an error: FireServer can only be called from the client
I’m trying to make the random events to happen with everyone at once, so it should be server, right? How do I make FireServer call from the client though?
Thanks once again for your help.

If you’re trying to make it fire to everyone you would fire these events on the server to the clients, using FireAllClients(), for example my random sound player, I fire the sound instance to all the clients and they play the sound. I’m sure this is what you’re looking for. Example :

local events = eventsFolder:GetChildren()

--// Loop and fire a random event every loop
while true do
    local randomEvent = events[math.random(1, #events)] --> Get random event from the folder of events.
    randomEvent:FireAllClients() --> Just an example, I don't know if your situation is client or server but you get the idea fire the random event.
    wait(15) --> Wait before looping and repeating the process. 
end

I started getting issues with ‘can be only used on client’ etc. but I think I finally got it fixed by making all scripts LocalScripts. But the LocalScripts don’t seem to work. At least nothing goes to the output.

local events = workspace.Events:GetChildren()

while true do
	local randomEvent = events[math.random(1, #events)]
	randomEvent:FireAllClients()
	wait(15)
end

On the end of the Events script where the actual events are placed in:
Well first I have functions function name() and scripts on what functions do.
I got 3 different functions for 3 different events.
Then at the end of the script:

script.one.OnClientEvent:Connect(redDisappear)
script.two.OnClientEvent:Connect(allSpeed)
script.three.OnClientEvent:Connect(jumpAll)

Yes, I do have my events named as one and two and three haha, don’t judge me.
After the Connect is the name of function.

I think all scripts should be fine? I’ve also made the functions print things into the output like ‘Function redDisappear active’ etc. but they don’t print.

Well the random event loop should be on the server-side in a Script. There shouldn’t be any error with that. As for the connections of OnClientEvent those should be in a Local Script I think that’s where you problem is. Example :

--// SERVER SCRIPT \\--
local events = workspace.Events:GetChildren()

while true do
	local randomEvent = events[math.random(1, #events)]
	randomEvent:FireAllClients()
	wait(15)
end

--// LOCAL SCRIPT \\--
script.one.OnClientEvent:Connect(redDisappear)
script.two.OnClientEvent:Connect(allSpeed)
script.three.OnClientEvent:Connect(jumpAll)

I’ve done the exact that.
No errors appear in the output anymore but the scripts also don’t work. Nothing comes to output (prints) and nothing happens.
I also tried to make it print the chosen event in the server script, but it only prints out nil, so it doesn’t even choose an event to happen. There probably is something wrong with the server script, but my brain is so corrupted and I have no idea what’s wrong.

--// LOCAL SCRIPT \\--
script.one.OnClientEvent:Connect(redDisappear)
script.two.OnClientEvent:Connect(allSpeed)
script.three.OnClientEvent:Connect(jumpAll)

Make sure these events are the same ones you fire on the server so like maybe put the events in a folder in the workspace and do that.

Didn’t work for some reason, but I tried using NumberValue instead.
I made the script to put the value as math.random(1,3) and then print the value (on Server Script).
Then on the LocalScript, I made it take the value and then fire the event
Example:

local value = workspace.EventToFire.Value --EventToFire is a NumberValue

if value == 2 then --If NumberValue is 2 by random then
   allSpeed() --Fire function called allSpeed()
end

It seems to print the values out correctly after each 15 seconds, but does not fire any events.
These scripts also don’t use RemoteEvents. Just a value.