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

How do I make my gamepasses randomize?

Asked by 4 years ago

So.. my friend helped my make this gamepass item randomizer... There are multiple scripts that has the same thing like this to randomize it. Every time I play a game I spawn with that weapon, then each time I die... I can't seem to change the weapon some how each time I reset. This script is really similar to my badge too and it does randomizes sometimes. IDK if it's just RNG being a butt, or if it's an error I'm having.

local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")

local gamePassID = 11480688

function onPlayerSpawned(player) 

    local hasPass = false

    local success, message = pcall(function()
        hasPass = MarketplaceService:UserOwnsGamePassAsync(player.userId, gamePassID)
  print("Has Game Pass for Gun")
    end)

    if not success then
        warn("Error while checking if player has pass: " .. tostring(message))
        return
    end

    if hasPass == true and game.Workspace:WaitForChild(player.Name):WaitForChild("5").Value == "" then
        game.Workspace:WaitForChild(player.Name):WaitForChild("5").Value = "Greatsword" 
        wait(.4)
    game.Lighting.GamepassFolder.Greatsword:clone().Parent = player.Backpack
 end
end 

game.Players.PlayerAdded:Connect(function(player)
  player.CharacterAdded:Connect(function()
    onPlayerSpawned(player)
  end)
end)

Players.CharacterAdded:Connect(onPlayerSpawned)
0
Well, there's no randomize section in your code. BestCreativeBoy 1395 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago

The code doesn't have randomizer component. The code will always select a Greatsword because it says:

game.Lighting.GamepassFolder.Greatsword:clone().Parent = player.Backpack

Instead, what you can do is set up a table with the names of the weapons/items.

local possibleItems = {"Item 1", "Item 2"} -- establish a list of possible items the user can get

And then randomly select an item from the list by replacing the earlier line of code with:

local value = math.random(#possibleItems) -- randomly selects a value from our table
local chosenItem = possibleItems[value] -- gets the name of the selected value

game.Lighting.GamepassFolder[chosenItem]:Clone().Parent = player.Backpack -- finds the item with the name and clones it into the backpack
0
@lightifieds, how do I know that the player owns that gamepass? isn't doing that messes up some gamepasses stuff? TheBuliderMC 84 — 4y
0
You're meant to replace `game.Lighting.GamepassFolder.Greatsword:clone().Parent = player.Backpack` with the line I posted above. lightifieds 40 — 4y
Ad

Answer this question