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

How can I add more assets, then have it pick a random asset?

local skateboardId = 93601062

local Tool = script.Parent;


function insert()

	
	local root = game:GetService("InsertService"):LoadAsset(skateboardId)


	local instances = root:GetChildren()
	if #instances == 0 then
		root:Remove()
		return
	end

	
	root.Name = "InsertedObject" .. skateboardId

	game:GetService("InsertService"):Insert(root)

	local t = game.Players.LocalPlayer.Character:FindFirstChild("Torso")

	root:MoveTo(t.Position + t.CFrame.lookVector * 8)

	Tool.Handle.Drop:Play()

end

1 answer

You can use a table to store multiple values under one variable. Tables are very useful in your case as they can store these values and use them when needed.

To make it pick a random value, you need to use math.random. Math.random is a mathematical function that generates a random number. You can use this to pick a random position from the table that can be used to reference the random ID value.

ids = {93601062,123456,123456} --Replace each 123456 with your ID, separate each ID with a comma.

local Tool = script.Parent;

function insert()
	local randID = ids[math.random(#ids)] --#ids is the number of values in the table
	local root = game:GetService("InsertService"):LoadAsset(randID) --Load the random ID.

	local instances = root:GetChildren()
	if #instances == 0 then
		root:Remove()
		return
	end

	root.Name = "InsertedObject" .. randID
	game:GetService("InsertService"):Insert(root)
	local t = game.Players.LocalPlayer.Character:FindFirstChild("Torso")
	if t == nil then return end --Make sure that the script doesn't error if the torso is not found
	root:MoveTo(t.Position + t.CFrame.lookVector * 8)
	Tool.Handle.Drop:Play()
end

Be sure to also call your function, otherwise it will not do anything when you test it in game.

I hope this answer helped you. If it did, please be sure to accept it.

P.S: Try to give some more insight on what you're doing next time.

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