How to locally remove a part?

  1. Hi, I currently have a script that checks for the part you collided with (in this instance the part is ‘v’) then debounce’s and temporarily changes its transparency; removing it from view. The issue I’ve confronted is that the debounce and transparent-property change affects all players, my intention is for the touched part to be “removed” for that singular client.
-- LocalScript in StarterPlayerScripts

local sound = workspace.Coins:FindFirstChild("Sound")
local debounces = {}

for i,v in pairs(workspace.Coins:GetChildren()) do
	if v:IsA('BasePart') then
		v.Touched:Connect(function(Hit)
			local player = game:GetService("Players"):GetPlayerFromCharacter(Hit.Parent)
				if player then
				if not debounces[v] then
				debounces[v] = true
				-- print(v) 
				sound:Play()
				v.Transparency = 1 
				game.ReplicatedStorage.CoinGiver:FireServer()
				wait(10)
				v.Transparency = 0
				debounces[v] = false
			end
			end
		end)
	end
end

-- excuse the indentation haha
1 Like

If you destroy an instance on the client it will only destroy for that person

  1. You can use CollectionService to get all the parts and detect the .Touched event.
  2. Did you try with multiple clients or just solo?
1 Like

My intention isn’t to destroy it, I temporarily change its transparency to “respawn” the part after x amount of time. Albeit, I could clone the part, however keeping the part in the workspace was just a preference.

1 Like

Yes, I tried with multiple clients. And I will give CollectionService a try.

1 Like

Probably because the Touched event fires for both clients, so it would make both delete the part locally. Try to do it again, and check on the server if the part transparency changed.

1 Like

The transparency isn’t changing on the server, so I believe you’re correct in that it’s firing for both clients. I tried to add a condition statement that checks if Hit.Parent == plr.Name, however it returns nothing. No errors.

local sound = workspace.Coins:FindFirstChild("Sound")
local debounces = {}
local plr = game:GetService("Players").LocalPlayer


for i,v in pairs(workspace.Coins:GetChildren()) do
	if v:IsA('BasePart') then
		v.Touched:Connect(function(Hit)
				if Hit.Parent == plr.Name then
				if not debounces[v] then
				debounces[v] = true
				print(v) 
				sound:Play()
				v.Transparency = 1 
				game.ReplicatedStorage.CoinGiver:FireServer()
				wait(10)
				v.Transparency = 0
				debounces[v] = false
			end
			end
		end)
	end
end

Try with CollectionService as i said, here is a tutorial that AlvinBlox’s made:

And this isn’t working because you are trying to find if hit.Parent (instance) is LocalPlayer’s Name, try with hit.Parent.Name == plr.Name instead.

1 Like
if Hit.Parent.Name == plr.Name then

Verifying the hit was the local player solved the issue and it no longer replicates to other clients. Thank you so much for your time.

1 Like

This detects if anyone touches it, but it sounds like you want it to only activate locally when it is touched by the local player?

if player and player == game.Players.LocalPlayer then