I have a model which follows the player.
It is meant to follow me, the owner of the game (or the player chosen)
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:GetPlayerByUserId(2422144525)
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)
Is there any way to make the model only choose a certain player? (The ID is my user)
I think your issue is that there is two variables with the same name. game.Players.PlayerAdded:Connect(function(player) and local player = Players:GetPlayerByUserId(2422144525). Try differentiating them with different names and then your code may work as intended.
local Players = game:GetService("Players")
local owner = Players:GetPlayerByUserId(2422144525)
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(owner)
end)
end)
will work because the variables are different names. It is currently giving it to the newest player in the server. If you don’t understand, this code should still do what you need it to.
Like CrimKing stated, your PlayerAdded event fires the ‘givePet’ function whenever a player just joined, so your pet is being torn by 2 versions of the function that run for each player. If all you’re trying to do is make it follow you, then try this:
local Players = game:GetService("Players")
local chosenPlayer = Players:GetPlayerByUserId(2422144525)
local pet = script.Parent
function givePet()
if chosenPlayer then
local character = chosenPlayer.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
givePet()
--game.Players.PlayerAdded:Connect(function(player)
-- player.CharacterAdded:Connect(function(char)
-- givePet(player)
-- end)
--end)