New instances only load when I leave workspace

I have an issue that’s easily replicable but i can’t seem to figure out how to fix, I have a script that creates a new camera and a new Model and inserts them inside of a viewport frame inside of starterGui, when i run the script it doesnt initially work, But when i exit workspace to go into the script editor everything loads.



This is a copy of the script :
local RunService = game:GetService(“RunService”)

script.Parent.MouseClick:Connect(function(player)
for _, v in pairs(game.Workspace.Poser:GetChildren()) do
if v:IsA(“Accessory”) or v:IsA(“BodyColors”) or v:IsA(“Shirt”) or v:IsA(“Pants”) then
v:Destroy()
end
end

local player = game.Players:FindFirstChild(game.Workspace.Ready.Player.Value)
if player then
	for _, v in pairs(player.Character:GetChildren()) do
		if v:IsA("Accessory") or v:IsA("BodyColors") or v:IsA("Shirt") or v:IsA("Pants") then
			local he = v:Clone()
			he.Parent = game.Workspace.Poser
		end
	end
end

wait(.1)

local newcam = Instance.new("Camera")
newcam.CFrame = script.Parent.Parent.CFrame
newcam.Parent = game.StarterGui.SurfaceGui.ViewportFrame

local viewportFrame = game.StarterGui.SurfaceGui.ViewportFrame
if viewportFrame then
	RunService.Heartbeat:Wait(1) 
	viewportFrame.CurrentCamera = newcam
else
	warn("Viewport frame not found.")
end

local clone = game.Workspace.Poser:Clone()
clone.Parent = game.StarterGui.SurfaceGui.ViewportFrame

end)

The StarterGui is what gets copied to your PlayerGui when your character starts. I think you would want to use the PlayerGui instead. You might also need to set the ResetOnSpawn to false in the SurfaceGui if you want the changes to persist when your player respawns.

1 Like

It’s supposed to be viewed by all the players, and i mean it does work, but i need to leave workspace

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