Please don’t. It’s reinventing the wheel and bad practice. It’s also more error-prone should a child of the workspace share a name with a player.
local playername = plr.Name
local playerworkspace = game.Workspace[playername]
-- vs
local character = plr.Character
-- Don't need a variable for plr even
I have a feeling what you posted isn’t your full script then, because something like this doesn’t refresh. Where are you putting all this code into? CharacterAdded? PlayerAdded? Another event? A loop?
The rest of that code is important. The only way this is refreshing is if the code is being called multiple times. I’m going to need to see that. Here’s something I can provide anyway, it’s changed code and not something I’d usually make, but this thread’s been extended for quite a bit now.
print("Called")
local character = plr.Character
local currentPosition = character:WaitForChild("HumanoidRootPart").Position
-- Change the morph here
local charAdded do
charAdded = plr.CharacterAdded:Connect(function (character)
character:WaitForChild("HumanoidRootPart").Position = currentPosition
charAdded:Disconnect()
end)
end
plr:LoadCharacter(true)
-- Clean up
Check your console when you run this code again. If you see “Called” multiple times, then the thread that’s responsible for running this is running multiple times and why your value keeps refreshing.
game.ReplicatedStorage.ChangeAvatar.OnServerEvent:Connect(function(plr, avrid)
local TrelloAPI = require(game.ServerScriptService:WaitForChild("TrelloAPI"))
local BoardID = TrelloAPI:GetBoardID("Keeping Board Name Private")
local ListID = TrelloAPI:GetListID("AVID",BoardID)
local CardID = TrelloAPI:GetCardID("AVID "..avrid,BoardID)
local CardInfo = TrelloAPI:GetCardInfo(CardID)
local ModuleID = CardInfo.desc
warn("Remote Fired")
local insert = game:GetService("InsertService"):LoadAsset(ModuleID)
-- The line that inserts a model is protected, they'd have to steal my trello to get access to insert service
insert.Parent = game.Workspace
local playername = plr.Name
local playerworkspace = game.Workspace[playername]
local morph = game.ServerStorage.StarterCharacter --Replace with path to desired Morph
local chngmorph = morph:Clone()
chngmorph.Parent = game.StarterPlayer
wait(0.1)
local curpos = playerworkspace.Head.Position
plr:LoadCharacter(true) --The player died, respawn them.
wait(1)
playerworkspace.Head.Position = Vector3.new(curpos)
wait(0.1)
chngmorph:Destroy()
end)
Okay. I think I might have something decisive. I went around, played in Roblox Studio and tried reproducing the code to the best of my ability to simulate the code with its current issues and find something else.
Drum roll for the long-awaited answer after so many replies and the fact that I didn’t read properly…
…you’re setting the position of the old character.
playerworkspace refers to the old character. When you call LoadCharacter, the character is destroyed and a new one is spawned. Therefore, you’re setting the position of the removed character.
Change playerworkspace in this line to this to make it work:
plr.Character
That was literally me-writing-my-code simulator. 25 replies and it turned out it was a simple error of using the wrong variable. How did I not catch that.
A note for code improvement: you’ll probably want to use CFrame over Position. CFrame will give you an exact set back to where you were, where setting Position goes until the specified Vector3 is unobstructed.