How do I make a local touch event

I’m trying to make a part that disappears when a player touches it for example, Player 1 touches a part and player 1 no longer can see the part, but player 2 who did not touch the part yet still sees the part.

This code is local script located in starter player scripts

local part = game.Workspace.SunFragment1.sunfragment – a part

part.Touched:Connect(function(PartTouched)
	if PartTouched.Parent:FindFirstChild("Humanoid")then
		part:Destroy()
	end
end)

I haven’t found any solutions yet to this problem.

3 Likes

This script should be going into the part or in workspace (recommend the part) and then adjust your values to fit, etc…

Then because this isn’t in game.Players.PlayerScripts, it should work better?

Make sure your script is where a local script can run (not workspace , serverstorage or serverscriptservice)

local part = game.Workspace.SunFragment1.sunfragment
local Player = game.Players.LocalPlayer

part.Touched:Connect(function(PartTouched) 
	if PartTouched.Parent.Name == Player.Name then -- If the players name = the player then it destorys the part on his client and not others
		part:Destroy()
	end
end)
6 Likes

You’re likely facing this issue because the .Touched event on each client will detect that the part was touched and destroy it when it’s touched, no matter which client touched it. Just check if the name matches the client’s name after it’s touched and before it’s destroyed.

if PartTouched.Parent.Name == game.Players.LocalPlayer.Name then
	part:Destroy()
end
1 Like

In which case, I have suggested with recommendation that he would place the local script within the part and adjust the values from there…

Local Scripts don’t work inside the workspace

They do work when you put them in parts?
edit: nvm no they dont

Only in the player character but not anywhere else inside the workspace.

1 Like

Thank you so much for the help, this took me a while to figure out but now I see what I have been doing wrong.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.