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

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

Asked by 9 years ago
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

Closed as Not Constructive by Vlatkovski, Spongocardo, UserOnly20Characters, and ImageLabel

This question has been closed because it is not constructive to others or the asker. Most commonly, questions that are requests with no attempt from the asker to solve their problem will fall into this category.

Why was this question closed?

1 answer

Log in to vote
0
Answered by 9 years ago

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.

Ad