I made a shop system that contains multiple if statements. Only the first if statement works and I don’t know why.
Script:
local player = script.Parent.Parent.Parent.Parent.Parent
local debounce = false
script.Parent.MouseButton1Click:Connect(function()
if script.Parent.Parent.Parent.PickedItem.Value == "_crowbar" then
if player.leaderstats.Cash.Value >= 125 and not debounce and not player.Backpack:FindFirstChild("Crowbar") and not player.Character:FindFirstChild("Crowbar") then
debounce = true
script.Parent.Text = "Purchased!"
game.ServerStorage.Crowbar:Clone().Parent = player.Backpack
player.leaderstats.Cash.Value = player.leaderstats.Cash.Value -125
script.Parent.Parent.Parent.PlaySound:FireClient(player, "rbxassetid://4525871712")
wait(.5)
script.Parent.Text = "Purchase"
debounce = false
end
if script.Parent.Parent.Parent.PickedItem.Value == "_grenade" then
if player.leaderstats.Cash.Value >= 25 and not debounce and not player.Backpack:FindFirstChild("Grenade") and not player.Character:FindFirstChild("Grenade") then
debounce = true
script.Parent.Text = "Purchased!"
game.ServerStorage.Grenade:Clone().Parent = player.Backpack
player.leaderstats.Cash.Value = player.leaderstats.Cash.Value -25
script.Parent.Parent.Parent.PlaySound:FireClient(player, "rbxassetid://4525871712")
wait(.5)
script.Parent.Text = "Purchase"
debounce = false
end
end
if script.Parent.Parent.Parent.PickedItem.Value == "_turret" then
if player.leaderstats.Cash.Value >= 325 and not debounce and not player.Backpack:FindFirstChild("Turret") and not player.Character:FindFirstChild("Turret") then
debounce = true
script.Parent.Text = "Purchased!"
game.ServerStorage.Turret:Clone().Parent = player.Backpack
player.leaderstats.Cash.Value = player.leaderstats.Cash.Value -325
script.Parent.Parent.Parent.PlaySound:FireClient(player, "rbxassetid://4525871712")
wait(.5)
script.Parent.Text = "Purchase"
debounce = false
end
end
end
end)
2 Likes
Trying putting all the code together by using elseif.
3 Likes
You are improperly ending/starting the if statements.
i.e:
This:
if script.Parent.Parent.Parent.PickedItem.Value == "_grenade" then
Is inside of the if statement for:
if script.Parent.Parent.Parent.PickedItem.Value == "_crowbar" then
Meaning it can never run.
Try this code out instead and let me know if it rectifies the situation:
local player = script.Parent.Parent.Parent.Parent.Parent
local debounce = false
script.Parent.MouseButton1Click:Connect(function()
if script.Parent.Parent.Parent.PickedItem.Value == "_crowbar" then
if player.leaderstats.Cash.Value >= 125 and not debounce and not player.Backpack:FindFirstChild("Crowbar") and not player.Character:FindFirstChild("Crowbar") then
debounce = true
script.Parent.Text = "Purchased!"
game.ServerStorage.Crowbar:Clone().Parent = player.Backpack
player.leaderstats.Cash.Value = player.leaderstats.Cash.Value -125
script.Parent.Parent.Parent.PlaySound:FireClient(player, "rbxassetid://4525871712")
wait(.5)
script.Parent.Text = "Purchase"
debounce = false
end
elseif script.Parent.Parent.Parent.PickedItem.Value == "_grenade" then
if player.leaderstats.Cash.Value >= 25 and not debounce and not player.Backpack:FindFirstChild("Grenade") and not player.Character:FindFirstChild("Grenade") then
debounce = true
script.Parent.Text = "Purchased!"
game.ServerStorage.Grenade:Clone().Parent = player.Backpack
player.leaderstats.Cash.Value = player.leaderstats.Cash.Value -25
script.Parent.Parent.Parent.PlaySound:FireClient(player, "rbxassetid://4525871712")
wait(.5)
script.Parent.Text = "Purchase"
debounce = false
end
elseif script.Parent.Parent.Parent.PickedItem.Value == "_turret" then
if player.leaderstats.Cash.Value >= 325 and not debounce and not player.Backpack:FindFirstChild("Turret") and not player.Character:FindFirstChild("Turret") then
debounce = true
script.Parent.Text = "Purchased!"
game.ServerStorage.Turret:Clone().Parent = player.Backpack
player.leaderstats.Cash.Value = player.leaderstats.Cash.Value -325
script.Parent.Parent.Parent.PlaySound:FireClient(player, "rbxassetid://4525871712")
wait(.5)
script.Parent.Text = "Purchase"
debounce = false
end
end
end)
5 Likes
Thank you, I was thinking about using elseif but I didn’t really know how to make multiple elseif’s, it kept getting understriked.
2 Likes