How would I make this do random?

So I have this tool in my image label and instead of just giving the teddy I want it to do random, so if there are more than 1 tool in the image label it will choose one of those random, not just the teddy.

here is the script Please edit it if u can:

script.Parent.Touched:Connect(function(hit) --Event fires when the part is tocuhed
	if hit.Parent:FindFirstChild("Humanoid") then --Confirms it is a character
		local player = game.Players:GetPlayerFromCharacter(hit.Parent) --Finds the player
		if player.Backpack:FindFirstChild("Tool") == nil then --Change tool to the name of the tool
			if player.leaderstats.Cash.Value >= 50 then --Checks for the right amount, change 'Cash' to the name of your stat
				game.StarterGui:WaitForChild("ScreenGui").Enabled = true --Change ScreenGui to the Gui name
				game.StarterGui.ScreenGui.Parent = player.PlayerGui --Inserts the Gui into the player so only they can see it
				player.leaderstats.Cash.Value = player.leaderstats.Cash.Value - 50 --Subtracting the cash
			end
		end
	end
end)
game.ReplicatedStorage.ToolPurchase.OnServerEvent:Connect(function(player)
	if player.Backpack:FindFirstChild("Teddy") then
		-- Object is there, do nothing
		return
	else
		local clonedTool = script.Parent.Teddy:Clone() 
		clonedTool.Parent = player.Backpack 
	end
end)
script.Parent.MouseButton1Click:Connect(function() --Detects for when a player clicks the Gui
	script.Parent.Rotation = math.random(-12,11)
	wait(1)
	script.Parent.Visible = false
	wait(0.001)
	script.Parent.Parent.TextLabel.Visible = true
	script.Parent.Parent.Teddy.Visible = true
	script.Parent.Parent.TextLabel.Text = "You got a Teddy!"
	
	wait(1)
	script.Parent.Parent.Teddy.Visible = false
	script.Parent.Parent.TextLabel.Visible = false
	
	wait(2)
	game.ReplicatedStorage.ToolPurchase:FireServer() --Fires the remote event (change ToolPurchase to the name of the remote event if it is named something different)
end)
  • Each script is in it’s own script. nothing is together.
1 Like

Assuming that you had a folder named “Tools” or something of the sort in ReplicatedStorage to clone this is the best way in my opinion to do this.

local toolFolder = game.ReplicatedStorage.ToolFolder:GetChildren()
local selectedTool = toolFolder[math.random(1, #toolFolder)
selectedTool:Clone().Parent = player.Backpack

Also, if you wanted to save a couple of lines you could do this instead of having an else statement added on.

game:GetService("ReplicatedStorage").ToolPurchase.OnServerEvent:Connect(function(player)
	if not player.Backpack:FindFirstChild("Teddy") then
		local toolFolder = game.ReplicatedStorage.ToolFolder:GetChildren()
        local selectedTool = toolFolder[math.random(1, #toolFolder)
        selectedTool:Clone().Parent = player.Backpack
	end
end)

sadly I did not have a folder in replicated storage. I had it in my image Label. could you make it work if all the tools that would be random selected are in the image label ? using my scripts editing them?

Just do the same thing but instead have this line set as the path to the place where you’re storing them. Make sure you keep :GetChildren().

PATH:GetChildren()

okay I got it but now when I have the folder I get 2 items example: sword and gun. instead of just 1 tool thats in the folder

Can you send the full code responsible for giving the items?