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 this script if a player touched a part then show GUI?

I am making a game whee when you touch a brick if will open a Gui to ask for conformation to teleport to the shop and it also says Worspace is not a valid member of DataModel

script.Parent.Touched:connect(function(player)
	local teleportgui = game.Worspace.TeleportGui:Clone()
	teleportgui .Parent = game:GetService("StarterGui")
end)

2 comments

0

There shouldn't be an error, to my knowledge. Also line 3..... Your copying to startergui which won't work, because the player must reset for it to appear, and it appears for all users too...

0

You're*

2 answers

✓ Accepted answer

Problem

I've caught 2 problems in your script.

  1. You made a typo.
  2. You parented the gui into "StarterGui" which is the gui which every player gets upon joining.
  3. You didn't even check if a player touches the brick instead of a normal part.

Solution

Just simply correct the worspace into workspace and try to get the player's PlayerGui

script.Parent.Touched:connect(function(part) -- the part which touched the brick
    local parent = part.Parent -- the parent of the part
    local player = game.Players:GetPlayerFromCharacter(parent) -- check if the parent is a player character
    if player then -- if the player matching the character is found
	    local teleportgui = game.Workspace.TeleportGui:Clone() -- i corrected the workspace for you <3
	    teleportgui .Parent = player.PlayerGui -- the folder for guis of the player
    end
end)

A tip (not a robux tip lol)

No need for that, but you should make the script check if the player already has the gui which will avoid giving the player multiple guis or you should put a debounce. http://wiki.roblox.com/?title=Debounce

Here is the option with the gui checker:

script.Parent.Touched:connect(function(part) -- the part which touched the brick
    local parent = part.Parent -- the parent of the part
    local player = game.Players:GetPlayerFromCharacter(parent) check if the parent is a player character
    if player then -- if the player matching the character is found
	    if not player.PlayerGui:FindFirstChild("TeleportGui") then -- if the gui is NOT found
		    local teleportgui = game.Workspace.TeleportGui:Clone() -- i corrected the workspace for you <3
		    teleportgui .Parent = player.PlayerGui -- the folder for guis of the player
		    teleportgui.Name = "TeleportGui" -- change the name, we are gonna search for it
	   end
    end
end)

Comment on this if your script still errors so i'll just check the script again just incase i made a mistake.

6 comments

0

Error:(5,32) Expected '(','{' or , got ':'

0

I have a question.... So when the player touches a part, it touches the player model and the parent the player model is the thing that contains Backpack, PlayerScripts, and etc.

0

@130363 Which script did you get the error on? The solution or the tip? And yes.

0

Oh, sorry. Change the line 5 in the second script into if not player.PlayerGui:FindFirstChild("TeleportGui") then -- if the gui is NOT found

0

Thanks

0

10/10

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