Trying to make a door that destroys itself only for the person that has touched it

I’m trying to make a door that destroys itself locally for the person that has touched it. My current script is a local script in ReplicatedFirst

game.Workspace:WaitForChild("SpawnArea").SpawnDoor.Touched:Connect(function(hit)
    if hit.Parent:findFirstChild("Humanoid") then
        game.Workspace.SpawnArea.SpawnDoor:Destroy()
    end
end)

This current script destroys the door for everyone after one person has touched it, I want it to only destroy for the person that touched it

Are you sure it’s a local script? Also you can put it in StarterPlayerScripts

I have put the LocalScript in StarterCharacterScripts and StarterPlayerScripts and both have yielded the same result. i tested it in studio and in game with an alt account: when i touched (opened) the door with one account it opened on the other account’s screen as well
image

You have to check whether the person who touched it is the LocalPlayer:

    if hit:IsDescendantOf(game:GetService("Players").LocalPlayer) then
        game.Workspace.SpawnArea.SpawnDoor:Destroy()
    end
1 Like

After making this edit the door stops opening; no errors in console

You need to add .Character to the end of .LocalPlayer so it gets the character in workspace.

    if hit:IsDescendantOf(game:GetService("Players").LocalPlayer.Character) then
        game.Workspace.SpawnArea.SpawnDoor:Destroy()
    end
1 Like

Try putting this script into StarterPlayerScripts:

local spawnDoor = game.Workspace:FindFirstChild("SpawnArea").SpawnDoor
spawnDoor.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChildOfClass("Humanoid") then
		spawnDoor:Destroy()
	end
end)

Destroy() now supposedly replicates so I wouldn’t use it. What you can do is send an event when touched to the client to make the door invisible and non-collidable.

Is the door anchored? ‘Cause it may be a network ownership problem. If it’s not anchored, it’s very possible that the player that gets the closest to the door also gains ownership on said door, so when destroying it on the client they also destroy it on the server.

Hi👋,

To do that, you need a Script, LocalScript and RemoteEvent

  • Add the script inside your door.

  • Add the Local Script inside of StarterGui

  • Add the RemoteEvent inside ReplicatedStorage

For the script, code this:

script.Parent.Touched:Connect(function(hit) 
      if hit.Parent:FindFirstChild("Humanoid") then
           local player = game.Players:GetPlayerFromCharacter(hit.Parent)

           game.ReplicatedStorage.RemoteEvent:FireClient(player)
      end
end) 

For the LocalScript, code this:

game.ReplicatedStorage.RemoteEvent.OnClientEvent:Connect(function() 
     workspace.SpawnArea.SpawnDoor:Destroy()
end) 

Hope i helped you,
Goodluck🍀

3 Likes

If your door is anchored, then ignore this comment. Otherwise, my best guess is that this is an issue of NetworkOwnership.

The nearest player to an unanchored object in your game always has some control over its position and movement properties by default. It’s annoying, but there’s IS a way to reverse that.

Try putting this in a serverscript (that means you should try it in a normal script, not a localscript):

local spawnDoor = workspace:WaitForChild("SpawnArea").SpawnDoor
spawnDoor:SetNetworkOwner(nil)

Or use RemoteEvents with FireClient

someone already said that, and based on the fact that it’s not marked as a solution, the poster is probably too lazy/incompetent to figure out how to use remote events.

Gave the topic owner the scripts and steps

you’d be surprised how many people are too lazy to deal with foreign objects, even if the answer is right in front of them

Works great! Thank you, it works exactly as i wanted it to after changing the LocalScript to:

game.ReplicatedStorage.DoorDelete.OnClientEvent:Connect(function() 
	game.Workspace:WaitForChild("SpawnArea").SpawnDoor:Destroy()
end)

RemoteEven5 to RemoteEvent
added WaitForChild to SpawnArea
renamed the RemoteEvent to DoorDelete to keep things organized :grinning:

1 Like

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