How do i get the player in a server script

There is actually a lot of questions about this

But my one is the toughest to solve,I do not want to use touched because tocuhed is not useful.

Secondly,Player added will also affect the experiement because my game is multiplayer

How do i get player if i cannot use the above methods,because i have a value that needed to be check before it can continue

can u explain ur problem a bit in detail? In which way you want to achieve the player? there are alot of methods and ways

1 Like

Basically,i did a player varaible and i use a remote event because that seems to be the only reliable one.

Are there any better methods in which i can obtain the player.

Btw sensor hit is not the player,it is actually a model so…

1 Like

check if hit.Parent actually exists first and then checking again if the parent is actually a character before finally doing the rest

1 Like

It did exists.Now my problem is the sensor hit part is random because there are a lot of parts in my main model.

This is actually a bus script(with passengers) .

1 Like

Well there are a ton of methods to get a player, but I will give you the simplest ones which I believe you are talking about.

--- Touch

script.Parent.Touched:Connect(function(Touched)
if game.Players:FindFirstChild(Touched.Name) then
end
end)

--- Join
game.Players.PlayerAdded:Connect(function(Player)

end)
1 Like

if I understood u correctly. You are basically checking if player touched the bus sensor. Your approach seems okay to me, a basic example would be

Sensor.Touched:Connect(function(hit)
     local Hum = hit.Parent:FindFirstChild("Humanoid")
     local plr = Hum and game.Players:GetPlayerFromCharacter(Hum.Parent)
     if plr then
        --plr is now a confirmed Player

end)

I said the sensor (Hit) is not player.It is actually a model of the bus part.

Now the problem is the sensor touched part is random for the (hit) parameter

1 Like

are u talking about hit.Parent or just hit? if only hit then its not possible that its a model because the documentation states that Touched event can only return basepart

Sorry,it is the hit.Parent. My mistake i am talking .

And also i posted that touched events and game.Players.PlayerAdded is unreliable because it will affect as there can be multiple models.

I try to follow you, but seems that not enough info is provided or “confusing”

Could you explain exactly, the behavior that your “bus”, “sensor”, “player interactions”, “scenario where this could happen”, “detailed mechanics” you are building.
Otherwise I find hard to understand what you mean.

Ok so this is what i am doing???

I am actually doing a bus game with passengers

And also I do not know why the NPC cannot teleport to the part and always like this

It literally just teleport to on top of the bus…I have no clue what is going on.How do i fix this???

If you really want to use .Touched you could make a table containing all the parts you want to ignore.
Example:

local IgnoreTable = {} -- Bus Model, or what the bus parts are parented to, you could also add individual parts
local Players = game:GetService("Players")

Sensor.Touched:Connect(function(Hit)
if not table.find(IgnoreTable, Hit.Parent) and not table.find(IgnoreTable, Hit) then -- Checking if the parent of the hit is in the ignore table and if the hit part itself is in the ignore table 

     if Hit.Parent:FindFirstChildOfClass("Humanoid") ~= nil then 
          local Player = Players:GetPlayerFromCharacter(Hit.Parent)
           -- Now you have the player and can continue your code

     end

else 
   return
end

Hopefully I understood your issue correctly.

You could also reverse this and make a table of Player Characters and find if Hit.Parent is in the table.

part.Touched:Connect(function(hit)
      if hit.Parent and hit.Parent:FindFirstChildOfClass("Humanoid") then

      local character = hit.Parent
      local player = game.Players:GetPlayerFromCharacter(hit.Parent
    
     end
end)

I still don’t really see why you’re so hesitant on using PlayerAdded, It won’t lag your game at all even if its multiplayer.

1 Like

Actually right i have solved the problem,i use a bindable event to get the player and i think i am not going to use hit.Parent because the hit.Parent will be different everytime as there are lots of parts combined.

I already know it is not gonna work because if i do that,when there is a second player added in,the newplayer varaible will changed because it is a new one and also this is a server script (Local script would be easier to get the player)(server script will be harder to get the player)(Also,server scrips are better for passengers script.