Greetings delevopers! So i tried to make a lootbox system, I wanted to give random guns when part is touched. And the problem is that only Tool1 gives to player… I couldn’t find any solution so, could anyone help me?
Here is my code:
local TweenService = game:GetService("TweenService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local Model = workspace.LootBox:GetChildren()
local tool1 = ReplicatedStorage.ToolsFolder.tool1
local tool2 = ReplicatedStorage.ToolsFolder.tool2
local tool3 = ReplicatedStorage.ToolsFolder.tool3
local Part = workspace.LootBox.Hitbox
local GunTable = {1, 2, 3}
Part.Touched:Connect(function(info)
local plr = Players:GetPlayerFromCharacter(info.Parent)
if plr then
local randomtool = math.random(1, #GunTable)
if randomtool == 1 then
tool1:Clone().Parent = plr.Backpack
if randomtool == 2 then
tool2:Clone().Parent = plr.Backpack
if randomtool == 3 then
tool3:Clone().Parent = plr.Backpack
end
end
end
end
for _, obj in pairs(Model) do
local TI = TweenInfo.new(2)
local Target = {Transparency = 1}
local Tween = TweenService:Create(obj, TI, Target)
if Tween then
spawn(function()
Tween:Play()
wait(5)
obj:Destroy()
end)
end
end
end)
thats because you put those 2 if statements in each other so that means if randomtools==1 then it will execute tool1:Clone() and check if randomtool==2 while its equal to 1
I made your code a little bit cleaner so its less confusing.
local TweenService = game:GetService("TweenService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local Model = workspace.LootBox:GetChildren()
local tool1 = ReplicatedStorage.ToolsFolder.tool1
local tool2 = ReplicatedStorage.ToolsFolder.tool2
local tool3 = ReplicatedStorage.ToolsFolder.tool3
local Part = workspace.LootBox.Hitbox
local GunTable = {tool1,tool2,tool3}
Part.Touched:Connect(function(info)
local plr = Players:GetPlayerFromCharacter(info.Parent)
if plr then
local randomtool = math.random(1, #GunTable)
GunTable[randomtool]:Clone().Parent=plr.Backpack
end
for _, obj in pairs(Model) do
local TI = TweenInfo.new(2)
local Target = {Transparency = 1}
local Tween = TweenService:Create(obj, TI, Target)
if Tween then
spawn(function()
Tween:Play()
wait(5)
obj:Destroy()
end)
end
end
end)