Math.Random function not firing in local script?

  1. I’m trying to make a case for my game, where a user purchases it and then get’s a random item sent to their backpack.

  2. Currently when I test the script in a normal script it functions normally, the problem is that to make the UI visible it has to be a local script and when I test it as a local script it returns nothing even though it’s supposed to print the number that math.random returned.

  3. I don’t know how to fix this, I expected it to run as it did in a normal script but it didn’t work. I haven’t finished the script but I’ve just tested the base script to make sure it doesn’t break(the worst part of programming is doing hours of work to find out your code is broken).
    This is my script:

local Part = script.Parent
local CD = Part.ClickDetector
local randomTable = math.random(1,6)
local UI1 = game.StarterGui:FindFirstChild("Chest1")
CD.MouseClick:connect(function(Player)
	print(randomTable)
	if randomTable == 2 then
		print(Player.Name.." Just unlocked a shotgun!!!! Congrats "..Player.Name)
		UI1.Enabled = true
		wait(5)
		UI1.Enabled = false
	end
	if randomTable == 1 then
		print(Player.Name.." Just unlocked a ak-47!!!!! Congrats "..Player.Name)
		UI1.Enabled = true
		wait(5)
		UI1.Enabled = false
	end
	if randomTable == 3 then
		print(Player.Name.." Just unlocked a M4A1!!!!! Congrats "..Player.Name)
		UI1.Enabled = true
		wait(5)
		UI1.Enabled = false
		if randomTable == 4 then
		print(Player.Name.." Just unlocked a SNIPER!!!!! Congrats "..Player.Name)
		UI1.Enabled = true
		wait(5)
		UI1.Enabled = false
		end
	end
end)

If you insert a click detector and put a normal script in a part the code will function fine(other than the UI as the UI can’t fire from a normal script) but if you fire it in a local script nothing appears to happen.
Any help is appreciated!

LocalScripts cannot run if they are parented inside the workspace

Instead, put your LocalScript in StarterPlayerScripts and define the Part where it really is:

local Part = workspace.PartThingyHereToClickOn
local Player = game.Players.LocalPlayer
local PlayerGui = Player:WaitForChild("PlayerGui")
local CD = Part.ClickDetector
local UI1 = PlayerGui:WaitForChild("Chest1")
CD.MouseClick:Connect(function()
    local randomTable = math.random(1,6)
	print(randomTable)
	if randomTable == 2 then
		print(Player.Name.." Just unlocked a shotgun!!!! Congrats "..Player.Name)
		UI1.Enabled = true
		wait(5)
		UI1.Enabled = false
	end
	if randomTable == 1 then
		print(Player.Name.." Just unlocked a ak-47!!!!! Congrats "..Player.Name)
		UI1.Enabled = true
		wait(5)
		UI1.Enabled = false
	end
	if randomTable == 3 then
		print(Player.Name.." Just unlocked a M4A1!!!!! Congrats "..Player.Name)
		UI1.Enabled = true
		wait(5)
		UI1.Enabled = false
		if randomTable == 4 then
		print(Player.Name.." Just unlocked a SNIPER!!!!! Congrats "..Player.Name)
		UI1.Enabled = true
		wait(5)
		UI1.Enabled = false
		end
	end
end)

Also haha funni connect is deprecated use Connect instead

A few things.

LocalScripts do not work in workspace, secondly, you are using StarterGui to get the Ui when you should be using PlayerGui. Also your math.random is outside of the event, meaning it’ll randomise once.

I think this should work for what is needed (regular script in the part), it should enable it for that player

local Part = script.Parent
local CD = Part.ClickDetector

CD.MouseClick:Connect(function(Player)
	local gui = Player.PlayerGui
	local UI1 = gui:FindFirstChild("Chest1")
	local randomTable = math.random(1,6)
	print(randomTable)
	if randomTable == 2 then
		print(Player.Name.." Just unlocked a shotgun!!!! Congrats "..Player.Name)
		UI1.Enabled = true
		wait(5)
		UI1.Enabled = false
	end
	if randomTable == 1 then
		print(Player.Name.." Just unlocked a ak-47!!!!! Congrats "..Player.Name)
		UI1.Enabled = true
		wait(5)
		UI1.Enabled = false
	end
	if randomTable == 3 then
		print(Player.Name.." Just unlocked a M4A1!!!!! Congrats "..Player.Name)
		UI1.Enabled = true
		wait(5)
		UI1.Enabled = false
	end
	if randomTable == 4 then
		print(Player.Name.." Just unlocked a SNIPER!!!!! Congrats "..Player.Name)
		UI1.Enabled = true
		wait(5)
		UI1.Enabled = false
	end
end)

But you can also optimise your code.
Instead of an if statement try:

local unlocks = {
   {
        name = "shotgun",
        item = ... - replace here
    }
}

CD.MouseClick:connect(function()
    local random = math.random(1,#unlocks)
    print(Player.Name.." Just unlocked a "..unlocks[random][name].."!!!! Congrats "..Player.Name)
    UI1.Enabled = true
    wait(5)
     UI1.Enabled = false
end)

Interesting. Will keep that in mind! I don’t use local scripts that much or UI much so it worked.

This works to, I might use this as well. Thanks guys!

I might later, just making a clunky version for now :slight_smile:

1 Like

Just keep in mind this as an example reference

A LocalScript is client sided only (Or what the player sees on their screen), if you were to define local Player = game.Players.LocalPlayer in the server side, then where the heck would that Player be exactly

It’s basically a Impossible Mode of “Where’s Waldo?”