I wanted to make a developer item giver that give random guns and colors from a folder, but it doesn’t work well. It gives me a random and it is the same for the rest of the game. This is my script:
local debounce = false
local ReplicatedStorage = game:GetService(“ReplicatedStorage”)
local folder = ReplicatedStorage.Weapons.HyperlaserColors:GetChildren()
local tool = folder[math.random(0, #folder)]
– Define function to give tool to the player
local function giveTool(player)
local backpack = player:FindFirstChild(“Backpack”)
tool:Clone().Parent = backpack
print(tool.Name)
–end
end
script.Parent.Button.Touched:Connect(function(hit)
local folder = ReplicatedStorage.Weapons.HyperlaserColors:GetChildren()
local tool = math.random(0, #folder)
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player then
if not debounce then
if player.Name == “ElectricPeaPvZ” then
debounce = true
script.Parent.Button.BrickColor = BrickColor.Red()
giveTool(player)
wait(10)
debounce = false
script.Parent.Button.BrickColor = BrickColor.new(“Lime green”)
end
end
end
return tool
end)
You will need to move line 4 local tool = folder[math.random(0, #folder)] into the giveTool function. When you define a variable, it won’t change unless you specifically made it do so.
You can try to make it such that if the Script returns a number that has already been previously called, it’ll “reroll” until it gets a different number. Here’s an example of what you could do
local lastNumber
-- Insert other code here...
script.Parent.Button.Touched:Connect(function(hit)
local folder = ReplicatedStorage.Weapons.HyperlaserColors:GetChildren()
local tool = math.random(0, #folder)
if not lastNumber or lastNumber == nil then
lastNumber = tool
else
repeat
tool = math.random(0, #folder)
until tool ~= lastNumber
lastNumber = tool
else
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player and not debounce and player.Name == "ElectricPeaPvZ" then --Remove excessive if statements!!
debounce = true
script.Parent.Button.BrickColor = BrickColor.Red()
giveTool(player)
wait(10)
debounce = false
script.Parent.Button.BrickColor = BrickColor.new(“Lime green”)
end
return tool
end)