So currently I have a local script that fires a remote when the player holds button1down, and stops firing it when the player lets go of the button1down. When the player holds button 1 down a server script spawns in parts, how would I delete these parts after the player stops holding button 1, I tried making the parts global variables but that didn’t work.
local script
What you could do is localize these variables outside of the OnServerEvent block, and then when you fire the Roar event with the false parameter (for example, instead of OnServerEvent:Connect(function(plr), you would do OnServerEvent:Connect(function(plr, holding), and then check the value of holding to see whether it is true or false.) you check for that and then destroy all of the parts. Hope this helps! If you need any more explanation let me know!
This is merely an idea but when you remote is fired you could store the spawned objects into a players table. This will keep it stored till you fire the RemoteEvent again which in the event that they do fire it again the objects will be deleted along with their index in the table.
This code isn’t meant to be used but i’m sure you could use it as a stepping stone to give you a better understanding on how to handle the spawned objects:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Remote = ReplicatedStorage:WaitForChild("RemoteEvent")
local PlayerParts = {}
local function HandleRemote(Player)
if not PlayerParts[Player] then -- If a player hasn't spawned any parts.
local NewTable = {}
for Index = 1, 10, 1 do -- just a quick example to show you how to add the parts to a table.
local NewPart = Instance.new("Part")
NewPart.Parent = workspace
table.insert(NewTable, NewPart) -- Insert the newly generated part (or cloned object in your case) to the table.
end
PlayerParts[Player] = NewTable -- Dinners ready, time to set the table.
else
for _, Part in pairs(PlayerParts[Player]) do -- Loop through the players index and delete the parts
Part:Destroy()
end
PlayerParts[Player] = nil -- Remove the players index from the table.
end
end
-- Also check for PlayerRemoving to remove their index in the table to prevent a memory leak.
Remote.OnServerEvent:Connect(HandleRemote)