We are both new to scripting and decided to attempt to create a game which gives a random gun sword and miscellaneous item on each spawn. We have tried pretty much every tutorial we could find and none seem to work if anyone knows how to help us with this I would be very grateful. :)
How does one create a randomizer game? Me and my friend need help.
1 answer
You could put all the weapons in a dedicated folder in ServerStorage or somewhere and use GetChildren() to get a table with all the weapons. You can use the math.random function to choose a random weapon from the table every time and clone it to the player
local ServerStorage = game:GetService("ServerStorage")
local Players = game:GetService("Players")
-- table with all the weapons
local allWeapons = ServerStorage.Weapons:GetChildren()
Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
-- whenever somebody spawns or respawns, this will run
-- select a random weapon, from 1 (the beginning of the table) to the last value in the table, clone it, and put it in the player's backpack
local randomWeapon = allWeapons[math.random(1, #allWeapons)]:Clone()
randomWeapon.Parent = player.Backpack
end)
end)
5 comments
All right ill try that
Where do i put the script? I'm assuming ServerScriptService
Ok it worked tysm been trying for hours lol (for anyone viewing this thread wondering where to but the script just put it in workspace)
You should put most server scripts in ServerScriptService unless the script is part of a model or something else that's in the world https://developer.roblox.com/en-us/api-reference/class/ServerScriptService
@PynMan, I recommend you accept @OfficerBrah's answer. This will let people know that this is a verified working answer and it will help increase his reputation on the site
This discussion is preserved as it was. New answers, comments, and votes are closed.

2 comments
Based off percentage or just plain random?
Plain random