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

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

Asked by 2 years ago
Edited 2 years ago

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)
0
Is this a LocalScript? Frometa1998 35 — 2y
0
Yes, sorry I should've put that in my question. delirik_1446 4 — 2y
0
Don't worry about it. I figured that out, anyway. Frometa1998 35 — 2y

1 answer

Log in to vote
0
Answered by 2 years ago

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.

0
Works just as I wanted it to. Thank you :) delirik_1446 4 — 2y
Ad

Answer this question