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

Making this give a random weapon on touch?

Ok so basically when they touch it will give thema random weapon out of serverstorage but it wont work.

RespawnTime = 1200
Debounce = false

script.Parent.Touched:connect(function (Hit)
    local HumanoidIsFound = Hit.Parent:FindFirstChild("Humanoid")
    local PlayerIsFound = game.Players:GetPlayerFromCharacter(Hit.Parent)
    if Hit.Parent and HumanoidIsFound and HumanoidIsFound.Health > 0 and PlayerIsFound then
        if not Debounce then
            Debounce = true
            local Weapon = game.ServerStorage(math.random())
            if Weapon then
                local newWeapon = Weapon:Clone()
                newWeapon.Parent = PlayerIsFound.Backpack
            else
                Debounce = false
                return
    
            end
            wait(RespawnTime)
            Debounce = false
        end
    end
end)


1 answer

Your problem is here:

game.ServerStorage(math.random())

This syntax is invalid. I believe you are trying to do something along the lines of this

(game.ServerStorage:GetChildren())[math.random(1,NumWeapons)]
--GetChildren creates a table of all Children inside ServerStorage, and then with the [] syntax we can access values of that table by their index.

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