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.

01local Gears = game.ServerStorage.Gears:GetChildren()
02 
03script.Parent.Touched:Connect(function(Hit)
04    if Hit.Parent:FindFirstChild("Humanoid") then
05        local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)
06        local Inventory = Player.Backpack
07        local RandomGear = math.random(1, #Gears) -- PROBLEM HERE
08        RandomGear.Parent = Inventory
09    end
10end)

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:

01local Gears = game.ServerStorage.Gears:GetChildren()
02 
03script.Parent.Touched:Connect(function(Hit)
04    if Hit.Parent:FindFirstChild("Humanoid") then
05        local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)
06        local Inventory = Player.Backpack
07        local RandomGearIndex = math.random(1, #Gears) -- PROBLEM HERE
08    local RandomGear = Gears[RandomGearIndex]
09        RandomGear.Parent = Inventory
10    end
11end)

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