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

Making a ScreenGui appear when part is touched?

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)

8 comments

0

Is this all of the code?

1

For the ScreenGui, yes. I have 2 other functions in the script but they're for different things.

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.)

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.

0

And when I remove the closing command at line 7, line 8 becomes incorrect.

0

Is this in a localscript?

1

No, a normal script. I was unsure as I had two other functions.

0

Any chance that the part is getting destroyed before the rest of the script can run?

1 answer

✓ Accepted answer

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)

13 comments

0

I have copied this exactly and the StarterGui does not seem to appear. I also changed it to ServerStorage.

0

Mind pasting the entire error?

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?

0

That would be nice. If there are no errors, then I suspect something else in the script is the problem.

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.

0

No, the part is only destroyed when it is touched. If I don't touch it, it moves as normal.

0

I updated the script.

0

I copied it exactly again, however now the part does not move randomly.

0

But does the gui work?

0

Nevermind, I got it to move again. However, the gui does still not pop up.

0

any errors?

0

Ah, I edited it a little and got it to work. Thank you for your help. :)

0

np.

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