Hello, I was creating a block that turns a players GUI visible when it is pressed, it ended up working but only once. After the UI appears and is closed, the UI seems to be locked into an invisible state. I have tried fixing it with different methods, but am not sure what the issue is. Any clue on how I messed up?
Physical Button (Normal script)
Teleporter = script.Parent
Debounce = false
function onTouched(hit)
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player and Debounce == false then
Debounce = true
print("Teleporter Hit")
script.Parent.Parent.Beep:Play()
wait(2)
player.PlayerGui.TeleporterBackground.Frame.Visible = true
Debounce = false
end
end
connection = Teleporter.Touched:connect(onTouched)
Close UI button (Local Script)
local Button = script.Parent
Frame = script.Parent.Parent --Goes to the frame that was made visible by the button
script.Parent.MouseButton1Click:Connect(function()
Frame.Visible = false
end)
This is happening because you are opening the GUI on a server script and closing it on a local script.
Because of this the server thinks the UI is already open (because it cant see changes made on the client) and as a result it doesn’t change the visibility of the UI
The best way to update and change this would be to fire a remote event when the part is touched to the client and have the UI opened using the local script. A script could look like this:
-- Server script
part.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild('Humanoid') then
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
game.ReplicatedStorage.RemoteEvent:FireClient(plr)
end
end)
-- Local script
game.ReplicatedStorage.RemoteEvent.OnClientEvent:Connect(function()
script.Parent.Visible = true
end)
I would recommend always handling your GUI changes on the client rather than the server and you should take a look at remote events on the API
I tried this, and put the LocalScript in the Frame I wanted to appear, but an error came up saying that the OnServerEvent could only be used in the server, and I’m not exactly sure what that means.
Read up on RemoteEvents like topdog mentioned. They’re very vital for all scripting. OnServerConnect, like the name implies, only connects on the server.
If you’re using a localscript, you can just do something like this
part.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild('Humanoid') then
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
script.Parent.Visible = true
end
end)