I need help with an ability that doesn't work for more than 1 person

So I made an ability that spawns a ball above the player’s head and kind of follows him that can be activated and deactivated

But when 2 or more players try to use it, if one is already spawned for a player and the other player presses click to try to spawn one for him then it spawns a ball on the other player’s location and if he presses click again then it despawns the other player’s working ball . How do I make it so both of them can have it and them using it doesn’t affect the other player.

this is the server script because on the local script it just detects if the player pressed click then fires it

-- Variables
local Remote = game:GetService("ReplicatedStorage"):FindFirstChild("Mori") -- this is for left click
local Remote2 = game:GetService("ReplicatedStorage"):FindFirstChild("MoriFire") -- this is for right click
local mori = game:GetService("ServerStorage"):WaitForChild("mori")

local cooldown = {}

mori.PrimaryPart = mori.Part
local Active = false
local RunService = game:GetService("RunService")


Remote.OnServerEvent:Connect(function(player) -- left click
    if Active == false  then
        if cooldown[player.UserId] ~= "onCooldown"  then
            
            local character = player.Character
            local humanoidroot = player.Character.HumanoidRootPart
             newMori = mori:Clone()
            connection2 = RunService.Heartbeat:Connect(function()
                if player.Character.Humanoid.RootPart ~= nil then
                    newMori:SetPrimaryPartCFrame(CFrame.new(character.HumanoidRootPart.Position + Vector3.new(0,5,0)))
                else
                    newMori:Destroy()
                    connection2:Disconnect()
                end
            end)
            
            newMori.Parent = workspace
            print("click")
            Active = true
        end
        
    elseif Active == true then
        print("active is now false ")
        newMori:Destroy()
        connection2:Disconnect()
        Active = false
        cooldown[player.UserId] = 'onCooldown'
        wait(10)
        cooldown[player.UserId] = nil
    end
    
    

Remote2.OnServerEvent:Connect(function (player) -- right click
    if Active == true then
        print("right clicked when active is true")
    end
end)

you can make a table, where the keys would be players, and the values would be another table keeping track of the clone, the connection, and if it is active. then replace the newMori and connection2 and Active variable with moriData[player].newMori, moriData[player].connection2, and moriData[player].Active.

also make sure that when a player joins, their info gets initialized, and when they leave, info for that player gets removed. and when they get removed be sure to destroy the instance and disconnect the connection if they exist. they would probably exist only when it is active.

also, declaring variables without the local keyword at the beginning will make it global, like if you defined the variable outside of any function or control statement.

and some things about your code, when you do this:

if moriData[player].Active == false  then
  (blockA)
elseif Active == true then
  (blockB)
end

you can instead do this:

if moriData[player].Active then
  (blockB)
else
  (blockA)
end
-- note that blockA and blockB are swapped

also checking if a Boolean value is true or false using == is unnecessary, you can just do booleanVar to check if it’s true and not booleanVar to check if it’s false.

1 Like

Ah I see, for now it does make sense so thank you! I’ll try that later.