Ontouch make gui visable

I made a script so when you touch on a part then it opens a frame. It works the first time but the second time does not work? There is no errors

local player = game:GetService("Players").PlayerAdded:Wait()
db = true
script.Parent.Touched:Connect(function()
	if db == true then
		player.PlayerGui.Side.TeleportFrame.Visible = true
		db = false
		wait(1)
		db = true
	end
end)

Assuming it is a server script here is the fixed version:
.Touched will return the part that touched it

local Players = game:GetService("Players")

local Debounce = false

script.Parent.Touched:Connect(function(Hit)
    local Player = Players:FindFirstChild(Hit.Parent.Name)
    if not Player then return end

    if Debounce then
        return
    else
        task.delay(1, function()
            Debounce = false
        end)
    end

    local PlayerGui = Player.PlayerGui
    PlayerGui.Side.TeleportFrame.Visible = true
end)

If this doesnt work let me know, not used .Touched in a while

it is doing the same thing, the first tiem works but not the second

Your closing the frame on the client, and opening it on the server.

I can recommend 3 things:
.Touched on the client instead
Fire a remote to the player who touched it to open the frame
Fire a remote to the server to close the frame

1 Like

Not to mention game:GetService("Players").PlayerAdded:Wait() is just gonna give you the first player it detects on playeradded so for any other players it will not work, the script i give you will work for multiple players.

i tired chaning the sxcript to local scriopt and it does not work

localscripts dont work in workspace, youd have to parent it under StarterPlayerScripts

i used a remote ewvent


1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.