Below is the script for a dailog shop. It is very similar to that found in roblox wiki here.
local dialog = script.Parent dialog.DialogChoiceSelected:connect(function(player, choice) local stats = player:FindFirstChild('leaderstats') if not stats then return end local gold = stats:FindFirstChild('Gold') if not gold then return end if choice == script.Parent.DialogChoice.ChoiceA then if gold.Value >= 500 then game.ReplicatedStorage.AK-47:Clone().Parent = player.Backpack gold.Value = gold.Value - 500 end
While this script works, I want to make it so if you buy the AK, it replaces a certain weapon in your backpack (example, if you have a healthkit and a assault rifle in your backpack, buying the AK replaces the assault rifle ONLY).
Thanks!
It's just another if statement
If this exists, do that. Except in this case, 'that' is destroying 'this'.
local dialog = script.Parent dialog.DialogChoiceSelected:connect(function(player, choice) local stats = player:FindFirstChild('leaderstats') if not stats then return end local gold = stats:FindFirstChild('Gold') if not gold then return end if choice == script.Parent.DialogChoice.ChoiceA then if gold.Value >= 500 then game.ReplicatedStorage.AK-47:Clone().Parent = player.Backpack gold.Value = gold.Value - 500 if player.Backpack:FindFirstChild("M4A4") then -- Assault Rifle. player.Backpack.M4A4:Destroy(); end end -- I'll just ignore the amount of missing ends and assume the script snippet is truncated.