I’m having a problem with moving a character by the humanoid. Lets use this example.
Player A: Buys character
Player B: Buys same character
Character goes to player B’s base. BUT, it does not stay there. It tries to go to Player A’s base.
It should not do that and instead should stay at player B’s base.
I think that it might be because of how I am handling moving characters to player bases but I am not exactly sure.
Code handling moving characters
function SpawnService.MoveToPlayerBase(characterModel: Model, targetPosition: Vector3)
if activeNPCs[characterModel] then
task.cancel(activeNPCs[characterModel])
activeNPCs[characterModel] = nil
end
local humanoid = characterModel:WaitForChild("Humanoid")
humanoid:MoveTo(characterModel.HumanoidRootPart.Position)
local targetCFrame = CFrame.new(targetPosition)
MoveNPC(targetCFrame, characterModel)
SpawnService.UnAnimate(characterModel.Humanoid.Animator)
end
function MoveNPC(targetCFrame: CFrame, Character: Model)
local HRP = Character.HumanoidRootPart
repeat
task.wait()
if not Character.Humanoid then return end
Character.Humanoid:MoveTo(targetCFrame.Position)
until not Character.Parent or (HRP.Position - Vector3.new(targetCFrame.Position.X, HRP.Position.Y, targetCFrame.Position.Z) ).magnitude <= 1
end
code handling buying character
function BuyService:BuyCharacter()
self.Prompt.Triggered:Connect(function(plr)
pendingPlayerPurchases[plr.UserId] = (pendingPlayerPurchases[plr.UserId] or 0) + 1
local function cleanup()
if pendingPlayerPurchases[plr.UserId] then
pendingPlayerPurchases[plr.UserId] = pendingPlayerPurchases[plr.UserId] - 1
end
end
local playerBase = Bases:FindFirstChild(plr.Name)
if not playerBase then
warn("Could not find player base for " .. plr.Name)
cleanup()
return
end
local charactersFolder = playerBase:FindFirstChild("Characters")
local bedsFolder = playerBase and playerBase:FindFirstChild("Beds")
if not charactersFolder or not bedsFolder then
warn("Base for " .. plr.Name .. " is missing a Characters or Beds folder.")
cleanup()
return
end
local currentCharacterCount = #charactersFolder:GetChildren()
local maxCharacterSlots = #bedsFolder:GetChildren()
local pendingCount = pendingPlayerPurchases[plr.UserId]
if (currentCharacterCount + pendingCount) > maxCharacterSlots then
warn("CANT BUY: SLOTS ALL FULL (including pending purchases) for " .. plr.Name)
cleanup()
return
end
local charData = CharacterData[self.Model.Name]
local plrProfile = DataMethods.GetProfile(plr)
if plrProfile.Data.Tix >= charData.Cost then
DataMethods.RemoveTix(plr, charData.Cost)
HidePromptEvent:FireAllClients(plr, self.Prompt)
local PadToMove = playerBase:FindFirstChild("CollectPad", true)
SpawnService.MoveToPlayerBase(self.Model, PadToMove.Position)
local characterPad = BaseService.AddCharacterToBase(self.Model, playerBase)
if characterPad then
local newGenerator = MoneyService.new(plr, self.Model, characterPad.Collect, plrProfile, 0)
if not DataMethods.playerMoneyServices[plr] then
DataMethods.playerMoneyServices[plr] = {}
end
table.insert(DataMethods.playerMoneyServices[plr], newGenerator)
print("MoneyService created for new character: " .. self.Model.Name)
else
warn("Could not find characterPad for " .. self.Model.Name .. ", MoneyService not started.")
end
self.Model.Parent = playerBase.Characters
self.Prompt:Destroy()
else
warn(plr.Name .. " does not have enough Tix for " .. self.Model.Name)
end
cleanup()
end)
end