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

Simple easy question. Anyway to stop the GUI Flashing?

Hello, working on Safezone GUI. When you step off the platform, the GUI goes away, but when you step on it.. it appears again. (Or at least that's what is suppose to happen) Instead, when I walk around the GUI flashes while I'm moving and touching the part, although it does go away when I step off the part it still stays and flashes when I'm on it. Anyway, if you guys think you may fix it. Any help greatly appreciated!

Here's my script:

Local Script

local Player = game:GetService('Players').LocalPlayer
local Safezones = game:GetService('Workspace'):FindFirstChild('AcademySafezones')


Safezones.Spawn.Touched:Connect(function()
	wait()
	Player:WaitForChild('PlayerGui'):FindFirstChild('Safezone').Enabled = true
end)


Safezones.Spawn.TouchEnded:Connect(function()
	Player:FindFirstChild('PlayerGui'):FindFirstChild('Safezone').Enabled = false
end)


Safezones.Academy.Touched:Connect(function()
	Player:FindFirstChild('PlayerGui'):FindFirstChild('Safezone').Enabled = true
	
end)

Safezones.Academy.TouchEnded:Connect(function()
	Player:FindFirstChild('PlayerGui'):FindFirstChild('Safezone').Enabled = false
	
end)

2 answers

The reason for the flashing is because you're using TouchEnded and Touched. What will happen is you'll have a character that will walk around and every time their feet hit the ground on the spawn the GUI will be enabled, and every time their feet leave the ground it will be turned off. This will lead to the flashiness when you're walking.

To solve this you could use a few methods...

##Magnitude

You could constantly check the player's magnitude in relation to the spawn part to see if they have gone outside of it's area or not.

Due to magnitude's properties you can use the x & z components of the position to tract a circular shape around your spawn, or you could add in the y component to track a 3D spherical shape. Here is an example...

local safeZones = CollectionService:GetTagged("SafeZone")

function trackCharacter(character)
	local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
	while true do
		wait(0.1)
								-- Can change the value to whatever fits the part size better
		local insideSafeZone = false
		for i,v in pairs(safeZones) do
											-- I'd mess around with this
			if (v.Position - humanoidRootPart.Position).Magnitude <= v:GetExtentsSize() then
				insideSafeZone = true
			end
		end

		if insideSafeZone then
			-- GUI Enabled
		else
			-- GUI Not Enabled
		end
		
	end
end

game.Players.PlayerAdded:Connect(function(player)		-- When player is added
	player.CharacterAdded:Connect(function(character)	-- When a new character is added
		trackCharacter(character)
	end)
end)

It is also recommended you turn your GUI's property ResetOnSpawn to false. It saves you a lot of trouble redefining it in your scripts.

Other methods could include Region3, or simply checking part sizes/positions in comparison to player's

Example of that...

local safeZones = CollectionService:GetTagged("SafeZone")

function trackCharacter(character)
	local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
	while true do
		wait(0.1)	
		local insideSafeZone = false
		for i,v in pairs(safeZones) do
			
			if (humanoidRootPart.Position.X < v.Position.X +v.Size.X/2) then
				if (humanoidRootPart.Position.X > v.Position.X - v.Size.X/2) then
					if (humanoidRootPart.Position.Z < v.Position.Z + v.Size.Z/2) then
						if (humanoidRootPart.Position.Z > v.Position.Z - v.Size.Z/2) then
							insideSafeZone = true
						-- GUI Enabled (This is a 2D check, doesn't check height)
						end
					end
				end
			end
		end

		if insideSafeZone then
			-- GUI Enabled
		else
			-- GUI Not Enabled
		end
	end
end

game.Players.PlayerAdded:Connect(function(player)		-- When player is added
	player.CharacterAdded:Connect(function(character)	-- When a new character is added
		trackCharacter(character)
	end)
end)

I used PlayerAdded events just to make it look a bit easier to read, but you'd likely do this in a local script if you're ONLY messing around with GUIs. In the local script the event catches would look more like this...

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()

function trackCharacter(character)
	...
end

spawn(function()
	 trackCharacter(character)
end)

player.CharacterAdded:Connect(function(newCharacter)
	character = newCharacter
	trackCharacter(character)
end)

1 comment

0

It didn't work, just tried it, I'm gonna post the script I made after what you gave me.

local player = game:GetService('Players').LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local CollectionService = game:GetService('CollectionService')

local safeZones = CollectionService:GetTagged("SafeZone")

function trackCharacter(character)
    local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
    while true do
        wait(0.1)   
        local insideSafeZone = false
        for i,v in pairs(safeZones) do

            if (humanoidRootPart.Position.X < v.Position.X +v.Size.X/2) then
                if (humanoidRootPart.Position.X > v.Position.X - v.Size.X/2) then
                    if (humanoidRootPart.Position.Z < v.Position.Z + v.Size.Z/2) then
                        if (humanoidRootPart.Position.Z > v.Position.Z - v.Size.Z/2) then
                            insideSafeZone = true
                        -- GUI Enabled (This is a 2D check, doesn't check height)
                        end
                    end
                end
            end
        end

        if insideSafeZone then
            player:WaitForChild('PlayerGui'):FindFirstChild('Safezone').Enabled = true
        else
            player:WaitForChild('PlayerGui'):FindFirstChild('Safezone').Enabled = false
        end
    end
end

game.Players.PlayerAdded:Connect(function(player)       -- When player is added
    player.CharacterAdded:Connect(function(character)   -- When a new character is added
        trackCharacter(character)
    end)
end)

1 comment

0

The playedAdded event won't work in a local script, you only want the character added event

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