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