What do you want to achieve?
I want to create a script that chooses a random event every 15 seconds to happen.
What is the issue?
Since I am not really great at scripting, I’m not really sure how to create the script.
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!
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.
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.
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:
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.
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.