Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why does this LocalScript loop even though there's no loop?

Asked by
Perci1 4988 Trusted Moderation Voter Community Moderator
10 years ago

I'm trying to replace the default character with a simple block.

Here is the hierarchy of the new character (the ClassName is shown in parentheses):

game
    ReplicatedStorage
        Brick (Model)
            Health (Script)
            Humanoid (Humanoid)
            Head (Part)
            Torso (Part)
                Mesh (BlockMesh)

Torso is a 2, 1, 4 sized brick. Head is the same size, on top of the torso, attached with surface welds.

When I put this model in workspace, then join the game in PlaySolo, then run the code

game.Players.Player.Character = Workspace.Brick

in the command bar, everything works perfectly. My character is changed to the brick, and I have complete control over it.

However, when I try to do this automatically with a LocalScript placed in StarterGui, everything goes wrong. The code loops over and over again, creating an infinite amount of new character blocks cloned from ReplicatedStorage. The print() at the beginning of my code also keeps firing, and my output is spammed. This doesn't make much sense to me since I have no loop in my code.

print("The code is running!")

local cam = workspace.CurrentCamera
local plr = game.Players.LocalPlayer
    repeat wait() until plr.Character

local newChr = game.ReplicatedStorage.Brick:Clone()
newChr.Parent = workspace
newChr:MakeJoints()
newChr:MoveTo(Vector3.new(plr.Character.Torso.Position))
plr.Character = newChr
cam.CameraSubject = newChr.Humanoid

Thanks in advance.

Answer this question