How to make random events?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want make random events for game, like Nuke or UFO
  2. What is the issue? Include screenshots / videos if possible!
    Here is a video, how I want to make: https://www.youtube.com/watch?v=8lS0FoiFfd0

Also, here is a place:
Testing events.rbxl (94.8 KB)

14 Likes

You should be going the right path with whatever this is. But, you’re trying to access the Disabled property of a Model? That’s not even a thing? Maybe you should consider handling it like the other events by cloning it into the workspace and destroying it after the event period.

6 Likes

It’s just that this task is difficult for me, and I decided to ask for help in Devforum

4 Likes

The script that performs the events is OK. But you’ll need to find a way to implement these events somehow, to make them do something.

Perhaps you can add a script in each event model that executes the actions needed, and it is automatically stopped when the event is destroyed. Then clone it into the workspace. You may have events that don’t need models, but you can just hardcode cases in the main script.

If the functionality of an event is the problem, you could’ve just made it the focus of this topic or made a new topic.

4 Likes

Sorry for question, but could you help me a little, please?

3 Likes

What do you need help with? Maybe just remove the IF condition for the nuke:

while true do
	local rnd_wait = math.random(100, 300) -- Random wait time between events
	local event = get_event_in_folder()

	print(event.Name .. " event triggered!")
	local clone_event = event:Clone()
	clone_event.Parent = workspace
	wait(event_period_time)
	clone_event:Destroy()

	-- Wait before the next event
	wait(rnd_wait)
end

Then you can just add a script into the event model that performs the actions you need. It will automatically start and stop when the model is cloned and destroyed respectively.

5 Likes

I’m confused sorry, idk how to do correctly

3 Likes

This is the simplest I can go, otherwise I have no idea what you want me to do here.

All you have to do is replace the while loop in RandomEvent with the one I gave you.
If you need the nuke to do something, make a script in the Nuke model and add what you want.
If you need the UFO to do something else (it looks complete to me), modify your scripts, add new ones, test around.

It’s really that easy.

3 Likes

I have script for nuke, to activate it

local nuke = game.ServerStorage.Nuke

local sus = game.SoundService["Tactical Nuke Incoming!"] 
local event_folder = game.ServerStorage.GameEvents
local event_peroid_time = 45 




--[[while task.wait() do 

local rnd_wait = math.random(100 , 300)
sus:Play()
wait(10)
nuke:Clone().Parent = workspace  
task.wait(rnd_wait)

end  ]] 



local function get_event_in_folder()
	local a = {}
	for _, event in pairs(event_folder:GetChildren()) do
		table.insert(a , event)

	end
	local random = math.random(1 , #event_folder:GetChildren())

	return  a[random]

end

while task.wait() do 
	local rnd_wait = math.random(100 , 300)	
	sus:Play()
	task.wait(10)
	local event = get_event_in_folder()
	local clone_event = event:Clone()
	clone_event.Parent = workspace		
	task.wait(event_peroid_time)
	clone_event:Destroy()

	task.wait(rnd_wait)





end
3 Likes

Also, I want to make random event like from that video https://www.youtube.com/watch?v=8lS0FoiFfd0

3 Likes

The RandomEvent script is already doing that (except the audio part – you’d move that into a script in the nuke)

Read up on SendNotification from SetCore to get notifications like in the game. I’m pretty sure that function is local only so you need to make use of RemoteEvents.

You’ll need to figure out how to spawn the meteorites yourself. All of this is too much for me to explain simply in this post. The Creator Hub documentation has good explanations.

1 Like

Maybe let’s try to do it together If you don’t mind?

2 Likes
function RNG(tableOfChances: {Instance: {number}})
	local totalChance = 0

	for _,chance in tableOfChances do
		totalChance += chance
	end

	local rng = math.random(1, totalChance)

	for option,chance in tableOfChances do
		rng -= chance
		if rng <= 0 then return option end
	end
end

local events = {
   [something] = number,
   -- add more
}

while true do
   local eventToOccur = RNG(events)
   -- do event
   local randomTimer = math.random(x,y)
   task.wait(randomTimer)
end
2 Likes

I need just copy this and paste right?

2 Likes

. . .

no

i gave you a “prototype”/“blueprint”, you need to make this code “compatible” with yours, how you do that is not up to me

i’m not gonna write you an entire script

4 Likes

This is not why the developer forum was created for

3 Likes