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

Random gear on touch isnt working ???

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

✓ Accepted answer

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.

2 comments

0

Np, I will try this.

0

This worked thx.

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