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 make the GUI appear and then go away?

How can I make the GUI appear when a player comes in contact with a brick and then go away when they move away from it. Someone told me it was with magnitudes but I tried it out and it didn't work for some reason. Since nothing popped up.

function touch(hit)
    if game.Players:findFirstChild(hit.Parent.Name) ~= nil then
        local player = game.Players[hit.Parent.Name]
        if player.PlayerGui:findFirstChild("Shop") == nil then
            local gui = script.Shop:clone()
            gui.Parent = player.PlayerGui
            repeat
                wait()
            until (player.Character.Torso.Position - script.Parent.Position).magnitude > 5
            gui:remove()
        end
    end
end
 
script.Parent.Touched:connect(touch)

1 comment

0

you can use :GetPlayerFromCharacter instead of game.Players[hit.Parent.Name], GetPlayerFromCharacter avoids the possibility of having another non-player object named the same as hit.Parent and selecting that instead of the player

2 answers

✓ Accepted answer

I don't think the way you are approaching this is a very efficient way, while server scripts are good for a lot of things, meddling with the player GUI isn't one of those. You should really just use a local script to make it a little easier. The reason being that server scripts can't directly modify the player GUIs. I say directly because server scripts can still give input through remote events, but those aren't necessary for this purpose.


Procedure


To accomplish this in a local script, you have to :

  1. Have a touched event to sense if something touched a part

  2. Check if the thing that hit the part is a descendant of the character of the local player

  3. Move a GUI on the screen

  4. wait a given amount of time, then move it off the screen

If you want to have a smooth transition, I'd recommend using tween service or the TweenPosition function of GUI elements.

local player = player.Players.LocalPlayer
local frame = script.Parent
workspace.Part.Touched:Connect(function(hit) -- 1
	if hit:IsDescendentOf(player.Character) then --2
		frame:TweenPosition(UDim2.new(0.3,0,.3,0))--3
		wait(2)--4
		frame:TweenPosition(UDim2.new(1,0,1,0))--4
	end
end)	

you can also change around the aesthetics of the tween by modifying the TweenPositon function:

GUI:TweenPosition(Udim2pos,easingDirection,easingStyle,time,overidable )

Ex: gui:TweenPosition(Udim2.new(0,0,0,0),Enum.EasingDirection.In,Enum.EasingStyle.Sine,2,true)

1 comment

0

Hmm I always wondered on adding TweenPositions, thanks! And yes I'll try local scripts

Use Visible unstead of remove, this is only for text boxes, text buttons, etc.

function touch(hit)
    if game.Players:findFirstChild(hit.Parent.Name) ~= nil then
        local player = game.Players[hit.Parent.Name]
        if player.PlayerGui:findFirstChild("Shop") == nil then
            local gui = script.Shop:clone()
            gui.Parent = player.PlayerGui
            repeat
                wait()
            until (player.Character.Torso.Position - script.Parent.Position).magnitude > 5
            gui.Visible = false
        end
    end
end

script.Parent.Touched:connect(touch)

9 comments

0

ScreenGui does not have a Visible property.

0

Yeah.. that doesn't work

0

use Enabled

0

Where do I make it enabled? The ScreenGui or in the script?

0

bruh :Remove() is deprecated use :Destroy()

0

I tried that before and it just makes the GUI appear on my screen without removing it whenever I leave from it. And when destroyed, it doesn't come back when I reset.

0

I thought it was a text box, sorry.

0

Actually nevermind I solved the problem because since I was using a different model like R16 it was supposed to be "HumanoidRootPart" instead of just "Torso"

0

Case Closed.

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