Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why does this math.random script always give me the same tool?

I'm trying to use a ClickDetector in a part to give the player who clicks it a random weapon, but everytime I click it I get the same thing.

Code:

local gamepassID = 510675750
local weapons = game.ServerStorage.Weapons:GetChildren()
local random = weapons[math.random(1, #weapons)]

script.Parent.ClickDetector.MouseClick:connect(function (playerWhoClicked)
	local player = playerWhoClicked
	local mps = game:GetService("MarketplaceService")
	local playerHasPass = mps:PlayerOwnsAsset(player, gamepassID)
	
	if playerHasPass then
		random:Clone().Parent = player.Backpack
	else
		mps:PromptPurchase(player, gamepassID)
	end
end)

Don't know why I'd have to say this, but yes, there is more than one tool in the folder (duh)

3 comments

0

the random variable won't change if you don't set it again. Also, try to set the randomseed (math.randomseed()) to tick(). (math.randomseed(tick()))

0

Put the "random" variable INSIDE the MouseClick function. That way it will change every time.

0

Put the "random" variable INSIDE the MouseClick function. That way it will change every time.

1 answer

When a "server" starts in ROBLOX studio, it always has the same math.random(), also why dont you call weapon inside the MouseClick function? That would also make the random right.

This discussion is preserved as it was. New answers, comments, and votes are closed.