How to make script detect player correctly?

I have a model which follows the player.
I want it to follow me (or any player chosen in the script),
But when someone else joins, it chooses them instead, and then glitches because it keeps deciding between me and the other player.

local Players = game:GetService("Players")
local player = Players:WaitForChild("Elvinwoman")

local pet = script.Parent

function givePet(player)
 if player then
  local character = player.Character
  if character then
   local humRootPart = character.HumanoidRootPart
   local newPet = pet:Clone()
   newPet.Parent = character
   
   local bodyPos = Instance.new("BodyPosition", newPet)
   bodyPos.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
   
   local bodyGyro = Instance.new("BodyGyro", newPet)
   bodyGyro.MaxTorque = Vector3.new(math.huge, math.huge, math.huge)
   
   while wait() do
    bodyPos.Position = humRootPart.Position
    bodyGyro.CFrame = humRootPart.CFrame
   end
  end
 end
end

game.Players.PlayerAdded:Connect(function(player)
 player.CharacterAdded:Connect(function(char)
  givePet(player)
 end)
end)

What do I have to change/fix for it to only go for the player chosen?

1 Like

When this Event fires, it should only fire for the Player it detected that joined, but I think the Issue here is that you assigned the Variable that has the same name to another value, which is probably why it is detecting it for all Players

1 Like

I don’t get what part is the same, pretty sure all of it is different…

1 Like

They are the exact same name, which would result in it being overridded by the new value.

And also, I recommend that you use UserId instead of waiting for a Player under a specific name.

1 Like

Updated the script:

local Players = game:GetService("Players")
local player = Players:GetPlayerByUserId("2422144525")

local pet = script.Parent

function givePet(user)
 if user then
  local character = player.Character
  if character then
   local humRootPart = character.HumanoidRootPart
   local newPet = pet:Clone()
   newPet.Parent = character
   
   local bodyPos = Instance.new("BodyPosition", newPet)
   bodyPos.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
   
   local bodyGyro = Instance.new("BodyGyro", newPet)
   bodyGyro.MaxTorque = Vector3.new(math.huge, math.huge, math.huge)
   
   while wait() do
    bodyPos.Position = humRootPart.Position
    bodyGyro.CFrame = humRootPart.CFrame
   end
  end
 end
end

game.Players.PlayerAdded:Connect(function(user)
 player.CharacterAdded:Connect(function(char)
  givePet(user)
 end)
end)

But this shows in Output now:

1 Like

Because you forgot to update player at the bottom of the code. It should be user.
If its intended, I suggest you check if its the Correct Player using their UserId

And also, this is not what I meant.

I meant this

if player.UserId == 2422144525 then
2 Likes

No more errors show, but it still cant decide which player.

Remember that wait() is deprecated and task.wait() is the correct method now! But to fix this script replace the bottom part with the following:

game.Players.PlayerAdded:Connect(function(player)
-- bad indentations since indentations don't exist when typing on the forum
 player.CharacterAdded:Connect(function(char)
    givePet(player)
 end)
end)

You accidentally named the player param as user or vice versa so when the thread is executed there’s an undefined variable there and the executor doesn’t know what to do with it so it halts execution.

tl;dr Change the player.CharacterAdded to user.CharacterAdded.

1 Like

Using this (also Midnightific’s fix)
Is this correct?

local Players = game:GetService("Players")

local pet = script.Parent

function givePet(player)
	if player.UserId == 2422144525 then
  local character = player.Character
  if character then
   local humRootPart = character.HumanoidRootPart
   local newPet = pet:Clone()
   newPet.Parent = character
   
   local bodyPos = Instance.new("BodyPosition", newPet)
   bodyPos.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
   
   local bodyGyro = Instance.new("BodyGyro", newPet)
   bodyGyro.MaxTorque = Vector3.new(math.huge, math.huge, math.huge)
   
   while wait() do
    bodyPos.Position = humRootPart.Position
    bodyGyro.CFrame = humRootPart.CFrame
   end
  end
 end
end

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(char)
		givePet(player)
	end)
end)

@Midnightific’s fix Is literally what I told you to do.

And yes, It all seems correct, but keep in mind that BodyPosition, and BodyGyro are Deprecated, you can shorten MaxTorque and MaxForce is simply this:

MaxForce = Vector3.one * math.huge
MaxTorque = Vector3.one * math.huge

, I also dont recommend you create a loop everytime the Player Respawns, Instead maybe Create a Connection, and Store it, so you can properly Manage it, and not have any performance issues.

1 Like

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