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

How do I make the GUI appear and then go away?

Asked by
fr2013 88
5 years ago

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)
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 theking48989987 2147 — 5y

2 answers

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

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)

0
Hmm I always wondered on adding TweenPositions, thanks! And yes I'll try local scripts fr2013 88 — 5y
Ad
Log in to vote
-1
Answered by 5 years ago
Edited 5 years ago

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)
0
ScreenGui does not have a Visible property. green271 635 — 5y
0
Yeah.. that doesn't work fr2013 88 — 5y
0
use ***Enabled*** greatneil80 2647 — 5y
0
Where do I make it enabled? The ScreenGui or in the script? fr2013 88 — 5y
View all comments (5 more)
0
bruh :Remove() is deprecated use :Destroy() AdminAyush 10 — 5y
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. fr2013 88 — 5y
0
I thought it was a text box, sorry. Gameplay28 139 — 5y
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" fr2013 88 — 5y
0
Case Closed. fr2013 88 — 5y

Answer this question