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)
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)
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.