My touch script won't work

I have created a script where a if a player touches a part (in this case Home) it makes a Button Visible. But sadly this script is not working :C, any help will be appreciated.


local TeleportGui = game.StarterGui.TeleoportGuis
local ShopTeleport = TeleportGui.ShopTeleport
local Home = script.Parent

function ShopTeleport()
    ShopTeleport.visible = true
    print("Fired ShopTeleport")
end

Home.Touched:Connect(ShopTeleport())

Remove parentheses from where you connect your function

Should look like

Home.Touched:Connect(ShopTeleport)

2 Likes
local TeleportGui = game.StarterGui.TeleoportGuis

Instead, of game.StarterGui, use

game:GetService("Players").LocalPlayer.PlayerGui.TeleportGuis

that should work.

Have a good day!

2 Likes

Were there any errors? It is a capital V for visible. It should be ShopTeleport.Visible.

1 Like

that might be it also no there wasn’t any errors in the output

Okay, I fixed your issue. This script uses a remote event and fires it from the part to a local script inside of the text button.

Script inside part that fires:

local TeleportGui = game.StarterGui.TeleportGuis
local ShopTeleport = TeleportGui.ShopTeleport
local Home = script.Parent
local event = game.ReplicatedStorage.RemoteEvent

function ShopTeleport(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		event:FireClient(player)
		print("Fired ShopTeleport")
	end
end

Home.Touched:Connect(ShopTeleport)

Local script inside text button that receives event:

game.ReplicatedStorage.RemoteEvent.OnClientEvent:Connect(function()
	script.Parent.Visible = true
end)
1 Like

This is a lot simpler:

‘’’
– yours

Home.Touched:Connect(ShopTeleport())

–mine
Home.Touched:Connect(ShopTeleport)
‘’"

Remove the “()”
Your passing the function in as its object, what your doing stm would be calling the function at the point that its trying to see what the variable should be passed.

Ez breezy beautiful, keep scripting you’ll get it!

2 Likes

Is this on a localscript or a serverscript?

If this is on the client then you should be using PlayerGui instead of StarterGui.

So you can replace local TeleportGui = game.StarterGui.TeleoportGuis, with local TeleportGui = game.Players.LocalPlayer.StarterGui.TeleoportGuis.

Another thing to point out is that you wrote Home.Touched:Connect(ShopTeleport()), instead of just Home.Touched:Connect(ShopTeleport), and it’s supposed to be ShopTeleport.Visible = true instead of ShopTeleport.visible = true, the visible starts with a capital V.

Also make sure that ShopTeleport is a Frame object, and not a ScreenGui.

This doesn’t work because you’re using StarterGui instead of PlayerGui. You also have 2 excessive parenthesis when calling the function to the .Touched event of the part.

In order to expand more about the PlayerGui, it is usually a child of the player in the Players services. But there is a catch: The player’s character touches the part instead of the Player itself, and PlayerGui isn’t really a descendant of the player’s character. So we can find the player using the character’s name (You can use GetPlayerFromCharacter but I am not really sure on how to use it at this time).

local Players = game:GetService("Players");
local Home = script.Parent

function ShopTeleport(hit)
    if hit.Parent:FindFirstChild("Humanoid") then --Check if it is a player by looking for the humanoid.
        local character = hit.Parent;
        local player = Players:FindFirstChild(character.Name); --Look for the player using the character's name.
        if player then --Confirm existence of player
            local PlayerGui = player:FindFirstChild("PlayerGui");
            local ShopTeleport = TeleportGui.ShopTeleport;
            ShopTeleport.Visible = true;
        end
    end
end

Home.Touched:Connect(ShopTeleport);

You may then use the TouchEnded event if you want to close the once the player goes away from the part.

Do this, you should remove the parentheses:

local TeleportGui = game.StarterGui.TeleoportGuis
local ShopTeleport = TeleportGui.ShopTeleport
local Home = script.Parent

function ShopTeleport()
    ShopTeleport.visible = true
    print("Fired ShopTeleport")
end

Home.Touched:Connect(ShopTeleport)

Using GetPlayerFromCharacter would be better and more secure

So you can modify the code to this:

local Home = script.Parent

function ShopTeleport(hit)
    if hit.Parent:FindFirstChild("Humanoid") then --Check if it is a player by looking for the humanoid.
        local character = hit.Parent;
        local player = game:GetService("Players"):GetPlayerFromCharacter(character)  -- Gets player object from the character
        if player then --Confirm existence of player
            local PlayerGui = player:FindFirstChild("PlayerGui");
            local ShopTeleport = TeleportGui.ShopTeleport;
            ShopTeleport.Visible = true;
        end
    end
end

Home.Touched:Connect(ShopTeleport);

Thank you! I did some mortifications to it and now it works! Thank you very much!

local Home = script.Parent

function ShopTeleport(hit)
	if hit.Parent:FindFirstChild("Humanoid") then --Check if it is a player by looking for the humanoid.
		local character = hit.Parent;
		local player = game:GetService("Players"):GetPlayerFromCharacter(character)  -- Gets player object from the character
		print("Checking For Player")
		if player then --Confirm existence of player
			local PlayerGui = player:FindFirstChild("PlayerGui");
			local ShopTeleport = PlayerGui.TeleportGuis.ShopTeleport;
			ShopTeleport.Visible = true;
			print("Made ShopTeleport Visible")
		end
	end
end

Home.Touched:Connect(ShopTeleport);
2 Likes