My local script randomizer is not working

Greeting’s. This local script that i have made is not working. The problem is that i need this local script to enable another local script using it’s randomizer. The issue is though whenever I start the game it doesn’t show any issues on the script output, and it doesn’t take my point of view to that random camera.

[The local script’s i need to enable with the randomizer are the ones with red lines next to them.]


local Scripts = game.StarterPack.Ram_cam 

local Cams2 = { --Table value.

local ScriptsActive = false;

local function random() 
	if not ScriptsActive then
		local ScriptsToActivate = math.random(1, 4); 
		ScriptsActive = true;
		Cams2[ScriptsToActivate].Disable = false;
		ScriptsActive = false; 


p.s: I didn’t find any other topics surrounding this.

1 Like

As far as I know, Scripts is not an event, so connecting a function to it will not run the function.

1 Like

What I would do is:

local Scripts = script:GetChildren() -- Get all the children scripts into a table

Scripts[math.random(#Scripts)].Disabled = false
1 Like

As @Quackers337 has said, a Script instance does not have a connection you can run code on other than when the server/client runs it on the join of the player / server start. Script Documentation. All you need to do is to just call the function at the end of your script.


Adding on to this, there’s no difference in how you index your array of scripts, the way OP provided, and what @Quackers337 provided do the same thing, just different ways of implementation.

1 Like

Good point. I’ll go check that out and see if i can change that.

Got it. I’ll try and do that now. Thank you.

Hello, very sorry about bothering you again but the script is not working again. And i am quite confused about this. Whenever i load up the game, i don’t get switched to the random camera. Here is the script: (Credit to You and desinied of course.)

local Scripts = script:GetChildren(
) -- Tables

Scripts[math.random(#Scripts)].Disabled = false

And the error i get when i load up the game:

Again sorry about replying so late.

local Scripts = script:GetChildren()
The GetChildren() function takes no arguments, it just gets all the the children objects.

Also you don’t need Random(Scripts), that’ll error since you don’t have a function called Random. Whats happening is that script:GetChildren() gets all the camera scripts and stores them in a table. Then Scripts[math.random(#Scripts)].Disabled = false is going to select a random element from that table of scripts and enable it.

First of all: I tried the script with and without the Random(Scripts) and I got the same error as before. However I do understand about the table that you are talking about. But Do you have any idea how I could do this properly then? I mean, if i want my script to be fully enabled and execute when a player spawns. I’ll just give you an example:

So let’s say the local script will activate when a player spawns and when it does, it will pick a random script from below and then enable that script. Then, the player’s camera will be changed to that random camera that the local script has picked.

p.s: I’m still fairly new to scripting, so forgive me for sounding like an idiot. And if you need me to explain further, i can if you wish.

Ahh sorry, I didn’t look at the error close enough. So your getting an interval empty error, which means the scripts haven’t loaded yet.
This will be fixed when you don’t run it instantly; so that might be done by connecting the code to an event.
If its when the player spawns it might look like:

local players= game:GetService('Players')
local plr=players.LocalPlayer

plr.CharacterAdded:Connect(function ()
	local Scripts = script:GetChildren()
	if #Scripts==0 then
		print('No scripts to select from')
	Scripts[math.random(#Scripts)].Disabled = false
1 Like

Work’s perfectly. I was stuck on this for an week. Thank you so very much for the info and the help! :grin:

1 Like