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

How do I get my surface gui to /correctly/ follow you around?

The previous title to this question was "How many pixels are in a stud"

What I'm trying to make now is a surface GUI that keeps track of your location. For example, it shows a red square wherever you're standing.

My problem now is getting the stud measurements of your character position to scale correctly to the pixel measurements of GUIs and I guess that number is something I need to know for some kind of conversion?

Right now it can only track you only if the part with the GUI is at 0,0,0-- my main concern right now is to just get it work with one player while scaling correctly.

Or if you know of a better way to accomplish that goal (Which I'm sure there is Dx) Just let me know what I'm /supposed/ to be doing. ;o;

local Frame = script.Parent.SurfaceGui.Frame
local Follow = Frame.Follow -- The red square that's supposed to follow you around
local Position = script.Parent.Position

repeat
	wait(1)
until game.Players:GetPlayers()[1] ~= nil

local Player = game.Players.Player1 
local Torso = Player.Character:WaitForChild("Torso")

while wait(0.1) do
	Follow.Position = UDim2.new(.5, (Position.Z - Torso.Position.Z * -1)*5, .5, (Position.X - Torso.Position.X)*5)
end

2 comments

0

So you want the part, that has the surface GUI attached to it, to follow you around? Can't you just set the position of the part to the player position -1 or something?

0

No no, the part has a gui on it, and I want the gui frame itself to follow you around. For example, if I knew how to I could make a dance floor that could make it look like water is rippling everywhere you walk.

2 answers

✓ Accepted answer

Alright I see what you mean. You want to have a small square on say, the baseplate, and have it follow the player. This is not so hard. There is no need for pixel math; SurfaceGuis have a property called CanvasSize which does it for you. Simply set the CanvasSize to the same size at its adornee, and 1 pixel will be equivalent to 1 stud. To get a bigger scale, you can multiply for that CanvasSize by 2 then you have 2 pixels per stud. ETC. I will work with 1xscale though.

To move the square to the player position, this is easy as well, only it can be confusing. As long as the surfacegui is centered on a part, you can create a new offset by subtracting the part position and player position. As for spacial differences, all I can say is make sure your UDim2 corresponds sanely to worldspace, by rotating the part. Do a lot of testing.

I managed to correspond: +world x = +x, and +world z = +y. Therefore, I should add world x to the x, and world z to the y. Actually quite simple!

Here is the code:

frame = script.Parent
gui = frame.Parent
part = gui.Parent
char = workspace:WaitForChild('Player1')
torso = char:WaitForChild('Torso')

while wait() do
	local p = torso.Position - part.Position
	frame.Position = UDim2.new(0.5,p.X,0.5,p.Z)
end

Hope I helped!

1 comment

0

By god if I knew what CanvasSize actually did earlier. This helped alot! I can do the rest myself now thanks!! ;D!!

Use billboard gui's, since they can be adorned to a part in the workspace, and this gui will always face the player's viewport. This way you can keep it as a static object and not having to worry about a ton of arbitrary (and very unoptimized loops) to do so.

1 comment

0

That... in no way solves my problem? I'm just having a hard time getting the maths right to get a /surface/ gui to follow you around. I don't see what a billboard gui has to do with it .-. I'm just gonna reword my question to something less absurd.

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