Trying to enable a script randomly

Hey, guys so I am trying to work out how to enable a random script. Essentially, I want to pick a random script from serverscriptstorage but this I don’t really know how to do it. I wrote this out and I think I’m doing it completely wrong.

ALL THE SCRIPTS ARE CURRENTLY DISABLED

local Picker = game.ServerScriptService.Picker
Bloom = game.ServerScriptService.Bloom
Blur = game.ServerScriptService.Blur
Color = game.ServerScriptService["Color Correction"]
Effect = game.ServerScriptService.Effect
Lighting = game.ServerScriptService["Normal Lighting (Black)"]

local randompick = {Bloom, Blur, Color, Effect, Lighting}
	
for i, v in pairs(randompick) do
    randompick.Disabled = false 
end

Don’t do that. Instead of randomly toggling scripts, handle the logic in the main script itself. I don’t know exactly what you’re attempting but at the very least, you should keep things within scripts without the need to use the disable property.

I’m trying to make it so, the script is randomly picked and enabled

Yes but that’s not exactly a specific use case. Are you trying to pick a random script to affect a game’s lighting? Same thing - handle it in-script. Example:

local effects = {
    ["Bloom"] = function()
        -- Set bloom
    end,
    ["Blur"] = function()
        -- Set blur
    end,
    -- ...
}

for _, effect in pairs(effects) do
    effect()
end

This isn’t exactly random though and neither is your script.

1 Like

Picking a random element to enable should be done using something like this:

local randompick = {Bloom, Blur, Color, Effect, Lighting}

local RNG = Random.new()

local nextPick = randompick[RNG:NextInteger(1, #randompick)]

nextPick.Disabled = false

What you are doing is enabling every script instead of just one. Also, since it looks like you are working with different lighting effects, you could probably just make this all a set of module scripts, and require/enable the random one you want, probably in a separate thread though:

local nextEffect = require(nextPick)
nextEffect:SetEnabled(true) -- something like that

colbert has the right idea though, just put all the lighting scripts as functions in a table and call one from it.

5 Likes

Thank you very much! It works now :slight_smile:

1 Like