Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Sizing/Creating local parts?

So I've created a script that sizes a group of parts proportional to the distance the player is from a certain part. The script works like this:

First I specify the player and the player's character (when it has loaded).

Next, I Activate a function that loops around a certain code block. The Code block itself...

  1. Creates an iteration (iterates through children in workspace)
  1. Finds Objects that are "Parts" and have a rotation value of 45,0,45
  1. Finds the distance between that part and the characters torso MINUS that part and the SpawningPad in that area (all using the magnitude property of a Vector3 value
  1. Sets the size of that part by setting each axis (of Vector3 value) as the distance that we just calculated above

The script is Local , and is located in the StarterPlayerScripts folder of the player. I have FilteringEnabled ON.

Here's the script:

player = game.Players.LocalPlayer

function SizeCubes(character)
	repeat wait() until character.Torso.CFrame ~= nil
	while player ~= nil and character ~= nil and character.Humanoid.Health ~= 0 do
		wait()
		local GetDistance
		for i,v in pairs(workspace:GetChildren()) do
			if v.ClassName == "Part" and v.Rotation == Vector3.new(45,0,45) then
				GetDistance = ((v.Position - workspace.SpawnPad.Position).magnitude) - ((character.Torso.CFrame.p - v.Position).magnitude)
				v.Size = Vector3.new(GetDistance,GetDistance,GetDistance)
			end
		end
	end
end

player.CharacterAdded:connect(function(character)
	repeat wait() until character.Torso.CFrame ~= nil
	SizeCubes(character)
end)

SizeCubes(player.Character)

So the main thing I'm getting at here is that it works on studio mode, but not online.

Yeah, yeah I've read the article on this problem, I'll read it again. But for now I want to focus on how you can create/edit local parts, obviously from a local script.

-Anyways, if you see any problems with my script alone, please add a comment. Otherwise, I'd like a detailed explanation on this concept, and maybe even an answer. THX!

1 comment

0

Is StreamingEnabled true in the Workspace? If so, it might be that some parts there are not replicated from the server to the client when you want them to be.

1 answer

✓ Accepted answer

There are a few things that I can point out.

First, I think that SizeCubes() should be passed into the CharacterAdded event without the overhead of the additional function you made. Since lines 18 and 4 are identical there is no need to have it.

player.CharacterAdded:connect(SizeCubes)

Second, the CFrame property of Torso is never nil, so line 4 will only cause wait() to be called once and the until loop ends.

Third, I think you should use the WaitForChild() method a little. I helped someone earlier where the CharacterAdded event was fired before even the Torso of the Character was replicated, causing a crash. It would be best to replace line 4 with:

local torso = character:WaitForChild("Torso")
local humanoid = character:WaitForChild("Humanoid")

And of course use the two variables further in the function where necessary.

1 comment

0

It actually turns out that the parts were'nt replicated onto a local folder (like a message or camera). I'll accept you answer anyways, since you did point out some good stuff :D .

This discussion is preserved as it was. New answers, comments, and votes are closed.