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

How would I use the Touched event to open up a gui which is in PlayerGui? (SOLVED)

So after realizing that you cannot use LocalPlayer inside of a normal script, I don't know how to go about doing this. Basically what this script is supposed to do is this: -A player touches a certain part/brick -A store gui which is located inside the player's PlayerGui opens (ShopGui1.Frame.Visible = true) -If the player stops touching the part, the gui closes (ShopGui1.Frame.Visible = false) This is what I had:

--Opens the store gui

function onTouch(hit)
	local human = hit.Parent:FindFirstChild("Humanoid")
	if human then
		game.Players.LocalPlayer.PlayerGui.ShopGui1.Frame.Visible = true
		script.Parent.TouchEnded:connect(endTouch)
	end
end

--Closes the store gui

function endTouch()
	game.Players.LocalPlayer.PlayerGui.ShopGui1.Frame.Visible = false
end

script.Parent.Touched:connect(onTouch)

1 answer

✓ Accepted answer

This might work. Basically, I've used :GePlayerFromCharacter to get the player of whoever touched the brick. Should work, but I haven't tested it as I am in college.

script.Parent.Touched:connect(function(Hit)
	if Hit.Parent:FindFirstChild("Humanoid") then
		local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)
		if Player then
			Player.PlayerGui.ShopGui1.Frame.Visible = true
		end
	end
end)

script.Parent.TouchEnded:connect(function(Hit)
	if Hit.Parent:FindFirstChild("Humanoid") then
		local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)
		if Player then
			Player.PlayerGui.ShopGui1.Frame.Visible = false
		end
	end
end)

2 comments

0

It worked, thanks!

0

No problem.

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