How can i delete a part with a localscript?

I want to know methods or the logic in how to delete parts (server parts) for a single player with a localscript

What is happening here is:

  • The Part i want to delete (Orb) is first located in ReplicatedStorage

image

  • When a player starts moving the Orb will be cloned to a folder in workspace and it’s position will be changed to the owner position (the person which is running) every 0.5 seconds
  • The Part (Orb) will have it’s name changed to the same name of the Owner

image

I’ve tried using a LocalScript inside the Orb but it doesn’t work and i am not getting how i should do this properly, also i’ve tried looking for local parts tutorials on youtube learning how to create local parts but not exactly how to delete server parts with local scripts.

The LocalScript i am using inside the part:

local plr = game:GetService("Player").LocalPlayer

script.Parent:GetPropertyChangedSignal("Name"):Connect(function()
	wait(0.1)
if script.Parent.Name == plr.Name then
	game.Workspace.BoostDots:FindFirstChild(plr.Name):Destroy()
end
end)
1 Like

Oh, now i understand, but yet, tried your solution and didn’t work, the orbs doesn’t dissapear and i am not getting anything on the output either

If you didn’t make any changes to the script and you only moved it, then yes you aren’t going to get any errors because it’s still a valid script. It would instead be checking for changes to the name of the PlayerScripts folder under the Player, from which StarterPlayerScripts populates.

In times like this I turn to my buddy, CollectionService, to help me out, because your script is now becoming a parts handler. The script creating the orbs and your new LocalScript will be working hand-in-hand to create a small system for handling your orbs.

First of all, for the script that adds the dots to the folder, add CollectionService and make sure that for every created orb, a tag is added to it. CollectionService:AddTag(Orb, "Orb") or something. Now your LocalScript will take charge from here.

In the LocalScript, you are going to have it listen for when a new instance is given the aforementioned Orb tag. You will then create a connection for the object that does what your original LocalScript does: create a name-check connection.

local CollectionService = game:GetService("CollectionService")
local LocalPlayer = game:GetService("Players").LocalPlayer

local function onNewOrb(object)
    object:GetPropertyChangedSignal("Name"):Connect(function ()
        wait(0.1)
        if object.Name == LocalPlayer.Name then
            object:Destroy()
        end
    end)
end

CollectionService:GetInstanceAddedSignal("Orb"):Connect(onNewOrb)
for _, orb in ipairs(CollectionService:GetTagged("Orb")) do
    onNewOrb(orb)
end

One change in your code: the function listening to the name change. I changed it to destroy that specific orb if its name is changed and matches the LocalPlayer’s after 0.1 seconds. I think this is the behaviour you want, but I’m not so sure, since your code was originally slightly confusing.

Your original code was flawed: not only did it assume the existence of the object it was trying to find (meaning if FindFirstChild returns nil, that will error), but it was also finding the first child of the BoostDots folder sharing the LocalPlayer’s name. That means that the orb you’re destroying may not even be the one which had its name changed, meaning the current orb for the function. You don’t need to do any extraneous work when you already have a reference to the orb itself.

3 Likes

I tried to follow your steps but probably didn’t do it right, just in case i’ll leave my Orb Spawner script here so you can see if i did it correctly:

local CollectionService = game:GetService("CollectionService")
local plr = game:GetService("Players"):GetPlayerFromCharacter(script.Parent)
local h = script.Parent:WaitForChild("Humanoid")
local Orb = game.ReplicatedStorage.Orb
local plrWalking = false

h.Running:Connect(function(speed)
	if speed > 0 then
		plrWalking = true
	else
		plrWalking = false
	
	end
	
end)

while true do
	if plrWalking then
		local clonedOrb = game.ReplicatedStorage.Orb:Clone()
		clonedOrb.Parent = game.Workspace.BoostDots
		clonedOrb.Position = script.Parent:FindFirstChild("Torso").Position
		clonedOrb.Name = script.Parent.Name
		CollectionService:AddTag(Orb, "Orb")
	end
	wait(0.5)
end

1 Like

Your implementation of CollectionService is correct here. Did you make changes to the LocalScript in StarterPlayerScripts as well? Furthermore, where are either of these scripts located? Try doing a bit of debugging using prints and the console to see if you can isolate where exactly it isn’t working. At what point does it stop working, destroying the orbs or cloning them? Information like that is needed.

Make sure to be as detailed as possible about the circumstances when something doesn’t work correctly so we have information to work from. I can’t exactly help you with no knowledge of the case.

Ok, let’s go by parts.

Basically just Copy Pasted your script, but all i did with it was add an “)” to it since it was missing:

image

  • The LocalScript is on StarterPlayerScripts
  • Orb Spawner Script is on StarterCharacterScripts

Didn’t try printing yet but my only issue here is trying to delete the orbs with the localscript, the spawn script works fine.

2 Likes

OH! I just realised the issue after re-reading your code and my own, because I know for a fact my code is not the problem. I just wrote code like this not too long ago. Haha, my bad for not catching that upon the first read through. I knew this not working was strange when everything’s set up properly.

This here. It’s adding the tag to your prefab orb, not the orb you cloned. Change the first argument to clonedOrb and it should function as expected.

Like this? it still doesn’t delete the orbs, i must be missing something :thinking:

while true do
	if plrWalking then
		local clonedOrb = game.ReplicatedStorage.Orb:Clone()
		clonedOrb.Parent = game.Workspace.BoostDots
		clonedOrb.Position = script.Parent:FindFirstChild("Torso").Position
		clonedOrb.Name = script.Parent.Name
		CollectionService:AddTag(clonedOrb, "Orb")
	end
	wait(0.5)
end
1 Like

Oh, wow, that’s interesting. I’m going to try to privately reproduce the issue in a private Studio window and see if I can investigate the problem myself. In the meantime, you can also try debugging around. I think I need to go hands on with this one so I can see the issue for myself and attack the problem rather than posting and turning this into a wild goose chase. I’ll let you know if I turn up anything.

2 Likes

Okay then, i really appreciate your help and patience, i am not giving up on this so easily, i’ll let you know aswell if somehow i find an solution to this even if i don’t use this method xD

2 Likes

So with a bit of testing, I’ve finally arrived at a conclusion: none of our code, aside from that small argument change that was needed, is actually broken. It’s all working as expected. The thing I noticed however, after checking from the LocalScript, is that the orbs are already named the LocalPlayer’s name. Their names don’t change, therefore don’t fire the changed event.

If the desired behaviour you’re looking for is to destroy any and all orbs created by the player’s movement after a select amount of time, I recommend using AddItem from Debris or delay (if you want to get really technical, a custom delay implementation with Heartbeat or Promises). If their names are supposed to change after contact with another user, you’re fine to leave it as is, you’ll just need another player to test with.

Aha! The issue was right under our noses.

Well, i never worked with AddItem or Delay before so i’ll need to do a little research on it.

But basically what i want to do is for the orbs to be deleted or become invisible just so the localplayer can’t see his own orbs since he can’t grab them and so it would be unecessary for them to show up.

Do they need to become invisible the instant they spawn? You could probably just destroy them the instant they’re added. The requirements will change a bit though because in this script, I realise, the client will end up locally destroying all orbs.

From the orb spawn script, change the tag adding part to this:

CollectionService:AddTag(clonedOrb, script.Parent.Name .. "Orb")

And from the LocalScript, updated code to account for the above:

local CollectionService = game:GetService("CollectionService")
local LocalPlayer = game:GetService("Players").LocalPlayer

local playerOrbName = LocalPlayer.Name .. "Orb"

local function onNewOrb(object)
    object:Destroy()
end

CollectionService:GetInstanceAddedSignal(playerOrbName):Connect(onNewOrb)
for _, orb in ipairs(CollectionService:GetTagged(playerOrbName)) do
    onNewOrb(orb)
end

You won’t need Debris, delay or the name changed signal with this. All that extraneous work can be cut right out and you can destroy objects that match the correct query right away. If you get an unexpected parent setting warning, you can add a small frame’s worth of delay before destroying them. Make a variable for RunService and then just above the Destroy part in onNewOrb, add in RunService.Stepped:Wait().

PS: In the server script, you might also want to switch the way you set the clonedOrb’s properties. Name first, position second, parent third. There’s some performance implications behind parenting first and then changing properties later.

3 Likes

And it worked xD

Well, that was basically what i was trying to do from the beggining, i think i explained it wrong or you got the idea wrong but that’s exactly what i wanted hahaha, Jeez i really need to research better on this stuff to understand how to code properly.

Again, thank your for your time and patience, i really appreciate it :slight_smile:

1 Like