Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Player and NPC doors not working?

Asked by 3 years ago

Hello.

I am currently trying to make a door that is both openable by an NPC, and the Player itself. Now, I am quite unsure how to fully make an NPC open a door, so instead, I made it so for the NPC, the door would open on touch, and for players, they'd have to click it. And this works, however the issue that I am having is that players can also open the door when touching them. I am unsure why this is being caused.

I have 2 scripts in the door, one to open the door by click (for the player) and one to open the door by touch (for the NPC). Now, of course, with this, it would do that because both scripts are running at the same time. I attempted to fix that by having a LocalScript in StarterCharacters scripts, that upon join, it would delete the door open touch script for the player only (locally).

However, that didn't work, and even after the script being removed for the player, the player could still open the door on touch even if the open door on touch script was deleted for the player.

I am quite unsure as to why this happens.

Here is the script:

local Door = workspace:WaitForChild("BedroomDoor")
local NPCOpenDoorScript = Door:WaitForChild("NPCDoorHandler")
wait(1)

NPCOpenDoorScript:Destroy()

If I could get some help with this, it would be great. Thanks!

1 answer

Log in to vote
2
Answered by 3 years ago
Edited 3 years ago

Every time the .Touched event fires, just check to see if the model that touched it is the character of a player, using :GetPlayerFromCharacter(). Keep in mind, you'll want to use a conditional statement, to make sure the player actually exists before acting upon it. Example:

local Trigger = workspace.Part

Trigger.Touched:Connect(function(part)
    local Character = part.Parent
    local Player = game:GetService("Players"):GetPlayerFromCharacter(Character)
    if not Player then --check to see if it's the NPC
        --open door
    end
end)

Note: you'll want to use an additional condition in the if statement, to make sure it's the NPC. I don't know what your set-up is, but checking if there's a humanoid is a good start.

1
This worked! Thanks alot! RazzyPlayz 497 — 3y
1
One more question, is the script responsive to players touching it? RazzyPlayz 497 — 3y
1
No, we determined that when we used `:GetPlayerFromCharacter()`. If you would like to add a snippet of code to run when a player touches it, simply add an `else` to the `if` statement! Gey4Jesus69 2705 — 3y
1
Ah okay, thanks! RazzyPlayz 497 — 3y
Ad

Answer this question