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

How do you make a code that opens a GUI when stepping on a part but closing it when you step off it?

I'm trying to make a shop and I put an invisible part infront of the shop so once a player stands on it, it will show the GUI and once they step off it will close the GUI. However my code doesn't even open the GUI and I'm not sure what the problem is.

I'm new to Roblox coding so im not sure how to make this happen. Here's my LocalScript code:

local GunShopFrame = script.Parent

GunShopFrame.Visible = false

workspace.openPart.Touched:Connect(function()
	GunShopFrame.Visible = true
end)

	
workspace.closePart1.Touched:Connect(function()
	GunShopFrame.Visible = false
end)

		
workspace.closePart2.Touched:Connect(function()
	GunShopFrame.Visible = false
end)

			
workspace.closePart3.Touched:Connect(function()
	GunShopFrame.Visible = false
end)

3 comments

0

Is this a LocalScript?

0

Yes, sorry I should've put that in my question.

0

Don't worry about it. I figured that out, anyway.

1 answer

✓ Accepted answer

There's multiple ways to go about this. I'm going to assume you're using a localscript, because you're referencing script.Parent, which is typically a GUI.

The best method would likely be a client event, which is basically an event that you can fire from the server to the client.

It's defined typically in localscripts.

game.ReplicatedStorage.gui_appear.OnClientEvent:Connect(function(vis)
	local appearing_frame = script.Parent -- Just reference the frame you want to appear.
	appearing_frame.Visible = vis
end)

On the server, you'd want to input the .Touched events, because it can only fire on the server.

workspace.openPart.Touched:Connect(function(hit)
	local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
	game.ReplicatedStorage.gui_appear:FireClient(plr,true)
end)


workspace.closePart1.Touched:Connect(function(hit)
	local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
	game.ReplicatedStorage.gui_appear:FireClient(plr,false)
end)


workspace.closePart2.Touched:Connect(function(hit)
	local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
	game.ReplicatedStorage.gui_appear:FireClient(plr,false)
end)


workspace.closePart3.Touched:Connect(function(hit)
	local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
	game.ReplicatedStorage.gui_appear:FireClient(plr,false)
end)

There is likely a better way to go about this, but this is the simplest.

1 comment

0

Works just as I wanted it to. Thank you :)

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