Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

[!!!!STILL UNANSWERED!!!!]Why does my script think I'm referencing the player instead of the npc?

When the gui button is clicked the server is supposed to receive a signal from a remoteEvent, which is working fine, but the accept visitor and refuse visitor functions are not working I keep getting this error: ' PrimaryPart is not a valid member of Player "Players.MOREHOURSOFFUN'

Why is this?

--Service Variables
local ls = game:GetService("Lighting")
local rs = game:GetService("ReplicatedStorage")

--Remote Events and Function Variables
local loadVisitGui = rs:FindFirstChild("loadVisitGui")
local acceptVisitor = rs:FindFirstChild("acceptVisitor")
local refuseVisitor = rs:FindFirstChild("refuseVisitor")

--NPC Variables
local visitorsFolder = rs:WaitForChild("Visitors")
local boy = visitorsFolder:WaitForChild("Boy")
local businessMan = visitorsFolder:WaitForChild("BusinessMan")
local oldMan = visitorsFolder:WaitForChild("OldMan")

--Model Variables
local door = game.Workspace.FrontHall.Front:WaitForChild("Door")

--Npc Spawn Variables
local visitorSpawn = game.Workspace:WaitForChild("visitorSpawn")
local insideHouse = game.Workspace:WaitForChild("insideHouse")

local function spawnVisitor(npc)
	wait(10)
	npc.Parent = game.Workspace
	npc.PrimaryPart.CFrame = visitorSpawn.CFrame
	wait(0.1)
	door.Transparency = 1
	return npc.Name
end

local function despawnVisitor(npc)
	npc = npc
	door.Transparency = 0
	npc.PrimaryPart.CFrame = CFrame.new(Vector3.new(-40.603, 3, -17.791))
	npc.Parent = rs
end

local function onAcceptVisitor(npc)
	npc = npc
	npc.PrimaryPart.CFrame = insideHouse.CFrame
end

local function visitorInteraction(npc)
	spawnVisitor(npc)
	loadVisitGui:FireAllClients(npc)
	
	acceptVisitor.OnServerEvent:Connect(onAcceptVisitor)
	refuseVisitor.OnServerEvent:Connect(despawnVisitor)
end

visitorInteraction(boy)

2 answers

When a signal is received from the client with OnServerEvent, the first parameter is always going to be the instance of the player who sent the signal.

It goes like this:

event.OnServerEvent:Connect(function(player, ...) -- where ... are the tuple arguments
    print(player.Name, "has called the server!")
end)

Therefore, if you are calling the server with :FireServer(npc), then your script just needs the simple change I made below

--Service Variables
local ls = game:GetService("Lighting")
local rs = game:GetService("ReplicatedStorage")

--Remote Events and Function Variables
local loadVisitGui = rs:FindFirstChild("loadVisitGui")
local acceptVisitor = rs:FindFirstChild("acceptVisitor")
local refuseVisitor = rs:FindFirstChild("refuseVisitor")

--NPC Variables
local visitorsFolder = rs:WaitForChild("Visitors")
local boy = visitorsFolder:WaitForChild("Boy")
local businessMan = visitorsFolder:WaitForChild("BusinessMan")
local oldMan = visitorsFolder:WaitForChild("OldMan")

--Model Variables
local door = game.Workspace.FrontHall.Front:WaitForChild("Door")

--Npc Spawn Variables
local visitorSpawn = game.Workspace:WaitForChild("visitorSpawn")
local insideHouse = game.Workspace:WaitForChild("insideHouse")

local function spawnVisitor(_, npc)
    wait(10)
    npc.Parent = game.Workspace
    npc.PrimaryPart.CFrame = visitorSpawn.CFrame
    wait(0.1)
    door.Transparency = 1
    return npc.Name
end

local function despawnVisitor(_, npc)
    npc = npc
    door.Transparency = 0
    npc.PrimaryPart.CFrame = CFrame.new(Vector3.new(-40.603, 3, -17.791))
    npc.Parent = rs
end

local function onAcceptVisitor(_, npc)
    npc = npc
    npc.PrimaryPart.CFrame = insideHouse.CFrame
end

local function visitorInteraction(_, npc)
    spawnVisitor(npc)
    loadVisitGui:FireAllClients(npc)

    acceptVisitor.OnServerEvent:Connect(onAcceptVisitor)
    refuseVisitor.OnServerEvent:Connect(despawnVisitor)
end

visitorInteraction(_,boy)

7 comments

0

i'm getting an "attempt to index nil with parent" on line 25, that's where i'm moving the npc from rs to workspace

0

How are you firing the server? I can't help if I can't see the client

0

I am firing the server when a button is clicked so for example "RemoteEvent:FireServer(npc)" so that fires when a button is clicked and is supposed to let the server know which npc they're dealing with

0

hello?

1

That's because you're calling 'spawnVisitor' when you haven't declared the NPC yet due to the Callbacks not being invoked by a signal pickup at the time.

0

nicely caught, thanks @Ziffixture

0

hopefully fixed...

I think I understood.

Error: PrimaryPart is not a valid member of Player "Players.MOREHOURSOFFUN '

Do you put the NPC in the Players folder on the server? This may be causing the Script to identify the NPC as a player, and the NPC does not have a PrimaryPart, so it returns an error each time it uses SetPrimaryPartCFrame.

Try to enter the body of the NPC and configure a PrimaryPart in the properties tab.

This discussion is preserved as it was. New answers, comments, and votes are closed.