Spell equip script not working properly. Why?

So I will make this simple. This script is for my spell game that allows you to equip a spell in any slot you wan’t. But the issue is I only want the player to be able to equip one spell in one slot. For example if “Lumos” is in slot 1 if you choose slot 2 it removes “Lumos” from slot 1 and adds it to slot 2 ONLY if it is an empty slot. For some reason i’m having trouble with this and require help. Thanks!

local occupiedSlots = {}

for i, v in pairs(EquipSpellsFrame.SpellSlots:GetChildren()) do
	if v:IsA("ImageButton") then
		v.MouseButton1Down:Connect(function()
			if SelectedSpell ~= nil then
				if v.Spell.Value == SelectedSpell then
					print("Unequipped " .. SelectedSpell)
					game.ReplicatedStorage.MainFolder.RemoteEvents.ServerInteractionEvent:FireServer("ChangeSpellGui",{Button = v.Name, Image = "", Spell = ""})
					occupiedSlots[v.Name] = nil
					
				elseif v.Spell.Value == "" then
					if occupiedSlots[v.Name] then
						local previousSlot = occupiedSlots[v.Name]
						print("Unequipped " .. SelectedSpell .. " from " .. previousSlot)
						game.ReplicatedStorage.MainFolder.RemoteEvents.ServerInteractionEvent:FireServer("ChangeSpellGui",{Button = previousSlot, Image = "", Spell = ""})
						occupiedSlots[previousSlot] = nil
					end
					
					print(occupiedSlots)
					print("Equipped " .. SelectedSpell)
					game.ReplicatedStorage.MainFolder.RemoteEvents.ServerInteractionEvent:FireServer("ChangeSpellGui",{Button = v.Name, Image = "rbxassetid://" .. SpellTextModule[SelectedSpell].Image, Spell = SelectedSpell})
					Sound("Equip")
					occupiedSlots[v.Name] = SelectedSpell
				else
					print("Slot already occupied")
				end
			end
		end)
	end
end

I see you’re managing spell slots in the game with the occupiedSlots table, however, you need to ensure one spell per slot by first checking if the target slot is empty before equipping a new spell. If not, remov the existing spell and then equip the new one.

local occupiedSlots = {}

for i, v in pairs(EquipSpellsFrame.SpellSlots:GetChildren()) do
    if v:IsA("ImageButton") then
        v.MouseButton1Down:Connect(function()
            if SelectedSpell ~= nil then
                if v.Spell.Value == SelectedSpell then
                    game.ReplicatedStorage.MainFolder.RemoteEvents.ServerInteractionEvent:FireServer("ChangeSpellGui", {Button = v.Name, Image = "", Spell = ""})
                    occupiedSlots[v.Name] = nil
                else
                    if v.Spell.Value == "" then
                        if occupiedSlots[v.Name] then
                            local previousSlot = occupiedSlots[v.Name]
                            game.ReplicatedStorage.MainFolder.RemoteEvents.ServerInteractionEvent:FireServer("ChangeSpellGui", {Button = previousSlot, Image = "", Spell = ""})
                            occupiedSlots[previousSlot] = nil
                        end

                        game.ReplicatedStorage.MainFolder.RemoteEvents.ServerInteractionEvent:FireServer("ChangeSpellGui", {Button = v.Name, Image = "rbxassetid://" .. SpellTextModule[SelectedSpell].Image, Spell = SelectedSpell})
                        occupiedSlots[v.Name] = SelectedSpell
                    else
                        local occupiedSpell = v.Spell.Value
                        game.ReplicatedStorage.MainFolder.RemoteEvents.ServerInteractionEvent:FireServer("ChangeSpellGui", {Button = v.Name, Image = "", Spell = ""})
                        occupiedSlots[v.Name] = nil

                        game.ReplicatedStorage.MainFolder.RemoteEvents.ServerInteractionEvent:FireServer("ChangeSpellGui", {Button = v.Name, Image = "rbxassetid://" .. SpellTextModule[SelectedSpell].Image, Spell = SelectedSpell})
                        occupiedSlots[v.Name] = SelectedSpell
                    end
                end
            end
        end)
    end
end

So for some reason it’s still allowing you to add one spell to multiple slots. I clicked a spell and added it to the top slot then click the right slot but it’s still in the top slot.

The issue might be related to the timing of events? I added a few prints to make sure the checks are in a correct order. You might also want to check other parts of your script to see if they’re interfering or anything, please inform me of any errors in the output or what shows in the output when using the system.

local occupiedSlots = {}

for i, v in pairs(EquipSpellsFrame.SpellSlots:GetChildren()) do
    if v:IsA("ImageButton") then
        v.MouseButton1Down:Connect(function()
            if SelectedSpell ~= nil then
                if v.Spell.Value == SelectedSpell then
                    print("Unequipped " .. SelectedSpell .. " from " .. v.Name)
                    game.ReplicatedStorage.MainFolder.RemoteEvents.ServerInteractionEvent:FireServer("ChangeSpellGui", {Button = v.Name, Image = "", Spell = ""})
                    occupiedSlots[v.Name] = nil
                else
                    if v.Spell.Value == "" then
                        if occupiedSlots[v.Name] then
                            local previousSlot = occupiedSlots[v.Name]
                            print("Unequipped " .. SelectedSpell .. " from " .. previousSlot)
                            game.ReplicatedStorage.MainFolder.RemoteEvents.ServerInteractionEvent:FireServer("ChangeSpellGui", {Button = previousSlot, Image = "", Spell = ""})
                            occupiedSlots[previousSlot] = nil
                        end

                        print("Equipped " .. SelectedSpell .. " to " .. v.Name)
                        game.ReplicatedStorage.MainFolder.RemoteEvents.ServerInteractionEvent:FireServer("ChangeSpellGui", {Button = v.Name, Image = "rbxassetid://" .. SpellTextModule[SelectedSpell].Image, Spell = SelectedSpell})
                        Sound("Equip")
                        occupiedSlots[v.Name] = SelectedSpell
                    else
                        local occupiedSpell = v.Spell.Value
                        print("Unequipped " .. occupiedSpell .. " from " .. v.Name)
                        game.ReplicatedStorage.MainFolder.RemoteEvents.ServerInteractionEvent:FireServer("ChangeSpellGui", {Button = v.Name, Image = "", Spell = ""})
                        occupiedSlots[v.Name] = nil

                        print("Equipped " .. SelectedSpell .. " to " .. v.Name)
                        game.ReplicatedStorage.MainFolder.RemoteEvents.ServerInteractionEvent:FireServer("ChangeSpellGui", {Button = v.Name, Image = "rbxassetid://" .. SpellTextModule[SelectedSpell].Image, Spell = SelectedSpell})
                        Sound("Equip")
                        occupiedSlots[v.Name] = SelectedSpell
                    end
                end
            end
        end)
    end
end

It’s so weird because equip and unequip works fine but when you choose a different slot with the same spell it will not remove from the previous slot. I’ll look further into this to see what my issue is.

Alright, I suggest adding checks always before applying the spell to a slot or also unequip the spell from the current spot equip it in the new one to ensure that each spell is only equipped in one slot at a time.