Multiple item scripting

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

	end
	script.Parent:Destroy()
end

end)

You can’t have two else statements in an if statement like that. Use elseif.

1 Like

I will but then it will have a error at one of the = signs

Please show us how your script looks like with elseif.

You need to learn the basics of if statements. Visit the DevHub here:

1 Like

here:

elseif is a conditional, and needs something to compare:

if 1 > 2 then
    print ("1 is greater than 2")
elseif 1 > 0 then
    print ("1 is greater than 0")
else
    print ("1 is not greater than 2 or 0")
end
3 Likes

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)

2 Likes

That does not work but thanks anyways

Its not plug and run code. You will need to adapt your system based on what I’ve written.

Alternatively, you could just do:

local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player then
    -- rest of the script
end
1 Like

what would I need to adapt and change

Where it says Item1, Item2, and Item3, put the items you want there. IE:

local ItemTable = {
[1] = game.ReplicatedStorage.sword1,
[2] = game.ReplicatedStorage.pistol1,
[3] = game.ReplicatedStorage.LazarGun
}

He has an actual system, not all of his items have a 1/3 chance.

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)

I just like to have more information in case I need to address the character at some point below. That’s totally fine, just preference.

His code was setup for a 50-50 chance, and he wanted to add a third item, so I assumed hes fine with 33% chance.

thank you so much this works perfectly

1 Like

No problem, good luck with your game!

1 Like