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.]
Script:
local Scripts = game.StarterPack.Ram_cam
local Cams2 = { --Table value.
Scripts:WaitForChild("Cameras"),
Scripts:WaitForChild("Cameras2"),
Scripts:WaitForChild("Cameras3"),
Scripts:WaitForChild("Cameras4")
}
local ScriptsActive = false;
local function random()
if not ScriptsActive then
local ScriptsToActivate = math.random(1, 4);
ScriptsActive = true;
Cams2[ScriptsToActivate].Disable = false;
print(Cams2[ScriptsToActivate])
wait(.1);
ScriptsActive = false;
end
end
Scripts:Connect(random);
p.s: I didn’t find any other topics surrounding this.
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.
random()
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.
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()
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')
return
end
Scripts[math.random(#Scripts)].Disabled = false
end)