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

Random gear on touch isnt working ???

Asked by 4 years ago

The following is suppose get a random gear when a part is touched and clone it to the players inventory. The problem is picking the random gear.

local Gears = game.ServerStorage.Gears:GetChildren()

script.Parent.Touched:Connect(function(Hit)
    if Hit.Parent:FindFirstChild("Humanoid") then
        local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)
        local Inventory = Player.Backpack
        local RandomGear = math.random(1, #Gears) -- PROBLEM HERE
        RandomGear.Parent = Inventory
    end
end)

1 answer

Log in to vote
1
Answered by 4 years ago
Edited 4 years ago

The problem here is that you only have the index for the random gear, not the gear itself. :GetChildren function gives you a table of children of an object. Here is your script's fixed version:

local Gears = game.ServerStorage.Gears:GetChildren()

script.Parent.Touched:Connect(function(Hit)
    if Hit.Parent:FindFirstChild("Humanoid") then
        local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)
        local Inventory = Player.Backpack
        local RandomGearIndex = math.random(1, #Gears) -- PROBLEM HERE
    local RandomGear = Gears[RandomGearIndex]
        RandomGear.Parent = Inventory
    end
end)

You might also want to add a debounce to your script if you don't want the player to get a lot of gears at once but I won't include that here.

0
Np, I will try this. THUNDER_WOW 203 — 4y
0
This worked thx. THUNDER_WOW 203 — 4y
Ad

Answer this question