Why isn't my remote event deleting these parts

I made a script that when the player equips the tool and has their button1down, it fires a remote and spawns parts in front of them and sendings true, but when the player lets go of button1 the script is supposed to delete the parts in front of them but it doesnt?

local script

tool = script.Parent

plr = script.Parent.Parent.Parent

mouse = plr:GetMouse()
script.Parent.Unequipped:Connect(function()
	equiped = false
end)
script.Parent.Equipped:Connect(function()
		equiped = true

	mouse.Button1Down:Connect(function()
		if equiped == true then
		game.Workspace.SCRIPTS.TOOLS.Frost.Roar:FireServer(true)
		
	mouse.Button1Up:Connect(function()
	
		game.Workspace.SCRIPTS.TOOLS.Frost.Roar:FireServer(false)
	end)
		end
	end)
end)

global script

local Players = game:GetService("Players")
local db = {}
debris = game:GetService("Debris")
Players.PlayerAdded:Connect(function(plr)
    db[plr] = true
end)
Players.PlayerRemoving:Connect(function(plr)
    db[plr] = nil
end)
script.Roar.OnServerEvent:Connect(function(plr,holding)
	if  db[plr] == true then 

	--db[plr] = false
	char = plr.Character
	char.Humanoid.WalkSpeed = 4
	_G.parts = {}
	_G.parts[plr] = {}
	bf1 = game.ReplicatedStorage.TOOLS.blueflame.BF1:Clone()
	bf2 = game.ReplicatedStorage.TOOLS.blueflame.BF2:Clone()
	bf3 = game.ReplicatedStorage.TOOLS.blueflame.BF3:Clone()
	bf4 = game.ReplicatedStorage.TOOLS.blueflame.BF4:Clone()
	table.insert(_G.parts[plr],bf1)
	table.insert(_G.parts[plr],bf2)
	table.insert(_G.parts[plr],bf3)
	table.insert(_G.parts[plr],bf4)
	while holding == true do  wait()
	bf1.CFrame = char.Head.CFrame * CFrame.new(0,0,-2.8)
	bf1.Parent = game.Workspace
	bf2.CFrame = bf1.CFrame * CFrame.new(0,0,-2.2)
	bf2.Parent = game.Workspace
	bf3.CFrame = bf2.CFrame * CFrame.new(0,0,-2.4)
	bf3.Parent = game.Workspace
	bf4.CFrame = bf3.CFrame * CFrame.new(0,0,-2.8)
	bf4.Parent = game.Workspace
		end
	end

end)
script.Roar.OnServerEvent:Connect(function(plr,holding)
	if holding == false then
	for i,v in pairs (_G.parts[plr]) do
		v:Destroy()
	end
	end
end)

For starters I would recommend using UserInputService, as I believe those events of the mouse object is deprecated, and this is unconfirmed but the entire mouse object could possibly be deprecated as well.

So your events would be the same as:

  local uis = game:GetService("UserInputService")


 local mouse_down = false
 uis.InputBegan:Connect(function(input, g)
        if not g and input.UserInputType == Enum.UserInputType.MouseButton1 then
           mouse_down = true
       end
  end)

    uis.InputEnded:Connect(function(input, g)
        if input.UserInputType == Enum.UserInputType.MouseButton1 then
           mouse_down = false
       end
  end

Other than this, Iā€™m sort of struggling to understand what your code is doing. If you could provide more details as to what the code does, and what it isnā€™t doing, and more on the goal, I may be able to help you further.

Basically when the player holds m1, parts spawn in and it follows the player, but when they let go of m1, theyā€™re supposed be destroyed in this line.

script.Roar.OnServerEvent:Connect(function(plr,holding)
	if holding == false then
	for i,v in pairs (_G.parts[plr]) do
		v:Destroy()
	end
	end
end)

however, this doesnā€™t work.

I just looked at your code again, and I believe the issue is because youā€™re running a while loop on the parameter of the event:

while holding

Hereā€™s the issue with that. Each time you fire a call to a server remote like this, it creates a new process of that body of code. It wonā€™t run hand and hand, if that makes sense. So from my perspective, Iā€™d think that loop would run forever, because if I call into the server with a ā€˜trueā€™ parameter, that loop will never stop running, and the more calls I make to the server most likely the laggier the game will become and you can probably expect weird things to occur.

Hope this helps a bit, I can try to work with your code a bit to provide an actual solution.

Iā€™ve rearranged your code a bit, Iā€™m not sure if this will work fully but the logic seems a bit better:

   local Players = game:GetService("Players")
     local db = {}
      _G.parts = {}
    debris = game:GetService("Debris")
   Players.PlayerAdded:Connect(function(plr)
    db[plr] = true
   end)
    Players.PlayerRemoving:Connect(function(plr)
    db[plr] = nil
    end)
   script.Roar.OnServerEvent:Connect(function(plr,holding)
if  db[plr] == true then 

--db[plr] = false
char = plr.Character
char.Humanoid.WalkSpeed = 4
    if not _G.parts[plr] then
_G.parts[plr] = {}
    end
if holding then
    bf1 = game.ReplicatedStorage.TOOLS.blueflame.BF1:Clone()
bf2 = game.ReplicatedStorage.TOOLS.blueflame.BF2:Clone()
bf3 = game.ReplicatedStorage.TOOLS.blueflame.BF3:Clone()
bf4 = game.ReplicatedStorage.TOOLS.blueflame.BF4:Clone()
table.insert(_G.parts[plr],bf1)
table.insert(_G.parts[plr],bf2)
table.insert(_G.parts[plr],bf3)
table.insert(_G.parts[plr],bf4)
bf1.CFrame = char.Head.CFrame * CFrame.new(0,0,-2.8)
bf1.Parent = game.Workspace
bf2.CFrame = bf1.CFrame * CFrame.new(0,0,-2.2)
bf2.Parent = game.Workspace
bf3.CFrame = bf2.CFrame * CFrame.new(0,0,-2.4)
bf3.Parent = game.Workspace
bf4.CFrame = bf3.CFrame * CFrame.new(0,0,-2.8)
bf4.Parent = game.Workspace
    else
     for I,v in pairs(_G.parts[plr]) do
         v:Destroy()
     end
    end

 end)

I gave you an example how to handle these exact thing in your previous post: How can I delete parts my script has created? - #4 by kinkocat

You also have no reason to be calling OnServerEvent twice for the same remote object. Seems like you are just overthinking it a little bit. You shouldnt even have to pass ā€œHoldingā€ into the remote aswell since its either ā€œonā€ or ā€œoffā€ if that makes sense.

The whole ā€œHoldingā€ thing seems to be redundant, check if the user has these spawned ā€œobjectsā€ and delete them if they do. Otherwise spawn them in.

Thanks for the insight, I see what i did wrong

The script worked, thanks:smiley:

1 Like