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

Making a ScreenGui appear when part is touched?

Asked by 8 years ago

I have been trying to make a ScreenGui appear when a Part is touched, however it does not seem to appear, and I think there may be an error in the code I am using. I appreciate anyone who can help me resolve this. Thanks.

local Model = game.Workspace.Part
function destroy()
    game.Workspace.Part:Destroy()
end
script.Parent.Touched:connect(destroy)

function move()
    local touched

    Model.Touched:connect(function(hit)
        if hit and hit.Parent:FindFirstChild("Humanoid") then
            touched = true
        end
    end)
    while not touched do
        Model.Position = 
        Vector3.new(math.random(),math.random(),math.random())*10
        wait(0.5)
    end
end
game.Players.PlayerAdded:connect(move)

function screen(Part)
local Gui = game:GetService("ServerStorage").ScreenGui
local Player = game.Players:GetPlayerFromCharacter(Part.Parent)
if Player and not Player.PlayerGui:FindFirstChild(Gui.Name) then
Gui:Clone().Parent = Player.PlayerGui
    end
end
script.Parent.Touched:connect(screen)
0
Is this all of the code? XAXA 1569 — 8y
1
For the ScreenGui, yes. I have 2 other functions in the script but they're for different things. toughjohncena1256 57 — 8y
0
what is the entire move function? Something seems to be missing (e.g. I don't know what "Part" is. There is a closing parenthesis at line 7 that shouldn't be there. Why is the Touch event connected to move instead of screen, etc.) XAXA 1569 — 8y
1
I apologize, let me edit that for you. I simply forgot to add screen instead of move, as my previous function was about moving the Part. toughjohncena1256 57 — 8y
View all comments (4 more)
0
And when I remove the closing command at line 7, line 8 becomes incorrect. toughjohncena1256 57 — 8y
0
Is this in a localscript? XAXA 1569 — 8y
1
No, a normal script. I was unsure as I had two other functions. toughjohncena1256 57 — 8y
0
Any chance that the part is getting destroyed before the rest of the script can run? XAXA 1569 — 8y

1 answer

Log in to vote
1
Answered by
XAXA 1569 Moderation Voter
8 years ago

The Touched event takes the Part it hit as its only parameter. You forgot to include that in the screen() function.

Also the part might have been destroyed before the rest of the script can run.

local Model = game.Workspace.Part

function move()
    local touched
    Model.Touched:connect(function(hit)
        if hit and hit.Parent:FindFirstChild("Humanoid") then
            touched = true
        end
    end)
    while not touched do
        Model.Position = 
        Vector3.new(math.random(),math.random(),math.random())*10
        wait(0.5)
    end
end
game.Players.PlayerAdded:connect(move)

function screen(Part)
    local Gui = game:GetService("ServerStorage").ScreenGui
    local Player = game.Players:GetPlayerFromCharacter(Part.Parent)
    if Player and not Player.PlayerGui:FindFirstChild(Gui.Name) then
        Gui:Clone().Parent = Player.PlayerGui
    end
    game.Workspace.Part:Destroy()
end
script.Parent.Touched:connect(screen)

0
I have copied this exactly and the StarterGui does not seem to appear. I also changed it to ServerStorage. toughjohncena1256 57 — 8y
0
Mind pasting the entire error? XAXA 1569 — 8y
0
There's no error in the script, it just doesn't seem to appear. Do you mean you want me to paste the whole script for you? toughjohncena1256 57 — 8y
0
That would be nice. If there are no errors, then I suspect something else in the script is the problem. XAXA 1569 — 8y
View all comments (9 more)
0
I edited it for you. The first function is to destroy, the second is for it to move randomly and the last is for the StarterGui to appear when touched. toughjohncena1256 57 — 8y
0
No, the part is only destroyed when it is touched. If I don't touch it, it moves as normal. toughjohncena1256 57 — 8y
0
I updated the script. XAXA 1569 — 8y
0
I copied it exactly again, however now the part does not move randomly. toughjohncena1256 57 — 8y
0
But does the gui work? XAXA 1569 — 8y
0
Nevermind, I got it to move again. However, the gui does still not pop up. toughjohncena1256 57 — 8y
0
any errors? XAXA 1569 — 8y
0
Ah, I edited it a little and got it to work. Thank you for your help. :) toughjohncena1256 57 — 8y
0
np. XAXA 1569 — 8y
Ad

Answer this question