Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 7 years ago
Edited 7 years ago

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)

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())) starlebVerse 685 — 7y
0
Put the "random" variable INSIDE the MouseClick function. That way it will change every time. jamesarsenault 192 — 7y
0
Put the "random" variable INSIDE the MouseClick function. That way it will change every time. jamesarsenault 192 — 7y

1 answer

Log in to vote
0
Answered by 7 years ago

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.

Ad

Answer this question