So, I have a nice loot script that I wrote and also got help with on this forum but when I made it I made it with only 2 items in the system. Now I want to add 3 items into it and I keep getting errors when doing so. Here is the script:
script.Parent.Touched:Connect(function(hit)
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player then
local randomNumber = math.random(1,100)
if randomNumber <= 50 then
game.ReplicatedStorage.sword1:Clone().Parent = player.Backpack
else
game.ReplicatedStorage.pistol1:Clone().Parent = player.Backpack
else
game.ReplicatedStorage.LazarGun:Clone().Parent = player.Backpack
I’d write it like this, because it allows you to add many items without a million if statements. (untested) :
local ItemTable = {
[1] = Item1,
[2] = Item2,
[3] = Item3
}
script.Parent.Touched:Connect(function(Hit)
local IsCharacter = Hit.Parent:FindFirstChild("Humanoid")
local Player = game.Players:FindFirstChild[Hit.Parent.Name]
if IsCharacter and Player then
local RandomItem = ItemTable[math.random(1,#ItemTable)]
RandomItem:Clone().Parent = Player.Backpack
script.Parent:Destroy()
end
end)
local items = {
game.ReplicatedStorage.sword1,
game.ReplicatedStorage.pistol1,
game.ReplicatedStorage.LazarGun
}
script.Parent.Touched:Connect(function(hit)
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player then
local itemClone = items[math.random(1, #items)]:Clone()
itemClone.Parent = player.Backpack
script.Parent:Destroy()
end
end)