I trying to make it so that if the player touches a Gui it teleports them to the place but the code isn’t working I am not getting any errors.
--Variables
local tele1 = script.Parent.Tele1
local debounce = false
--Functions
game.StarterGui.Ships.Holder.Button.MouseButton1Click:Connect(function(clicked)
if clicked.Parent:FindFirstChild("Humanoid") and debounce == false then
print("YES")
local char = clicked.Parent
local humanoidrootpart = char.HumanoidRootPart
humanoidrootpart.Position = tele1.Position
debounce = true
wait(1)
debounce = false
end
end)
--Variables
local tele1 = script.Parent.Tele1
local debounce = false
local player = game.Players.LocalPlayer
local playergui = player:WaitForChild("PlayerGui")
local shipUi = playergui:WaitForChild("Ships")
local Holder = shipUi:WaitForChild("Holder")
local Button = Holder:WaitForChild("Button")
--Functions
Button.MouseButton1Click:Connect(function(clicked)
if clicked.Parent:FindFirstChild("Humanoid") and debounce == false then
local char = clicked.Parent
local humanoidrootpart = char.HumanoidRootPart
humanoidrootpart.Position = tele1.Position
debounce = true
wait(1)
debounce = false
end
end)
Mhm thats odd, I remember adding a click parameter and adding an if statement to check whether it has been clicked and it worked, maybe it ignored that part because the localscript already knows that the player can only click it.
--Variables
local tele1 = game.Workspace.Map.TeleportShip.Tele1
local debounce = false
local Button = script.Parent
--Functions
Button.MouseButton1Click:Connect(function(clicked)
if clicked.Parent:FindFirstChild("Humanoid") and debounce == false then
local char = clicked.Parent
local humanoidrootpart = char.HumanoidRootPart
humanoidrootpart.Position = tele1.Position
debounce = true
wait(1)
debounce = false
end
end)
Yes of course you are, because you are trying to use clicked.Parent, when clicked is nil!
clicked will always be nil because MouseButton1Click doesn’t supply the clicked parameter.
If you are using a LocalScript you can simply get the player which clicked on the button with game.Players.LocalPlayer. If you are using a server script, you cannot find which player clicked the button (if it is a screen gui).
If you are using a surface gui, you cannot get the player that clicked on the button unless the gui is a descendent of PlayerGui
Why not? using a Remote Event allows you to get the Local Player, you can find out who clicked it.
that was probably a Click Detector then, it passes the player who clicked it as a parameter. The MouseButton1Click event fires when a player left clicks a Gui object, there’s no parameters passed.
@EHGTR1903 your code seems very redundant, there’s lots of unnecessary stuff you’re doing.
Code in this whole part won’t even run, because on the first line where you bind the event to a function, you pass a parameter.
Your code will work if you don’t try to pass a ‘clicked’ parameter in the function and instead do this
local player = game:GetService("Players").LocalPlayer
Button.MouseButton1Click:Connect(function()
--// this is happening locally, no need to pass a player object as a param.
local char = player.Character or player.CharacterAdded:Wait()
-- rest of your code
end)
also remember that instead of checking whether debounce == false
you can simply do
--Variables
local tele1 = script.Parent.Tele1
local debounce = false
--Functions
game.StarterGui.Ships.Holder.Button.MouseButton1Click:Connect(function(clicked)
if clicked.Parent:FindFirstChild("Humanoid") and debounce == false then
print("YES")
local char = clicked.Parent
local humanoidrootpart = char.HumanoidRootPart
humanoidrootpart.CFrame = tele1.CFrame
debounce = true
wait(1)
debounce = false
end
end)
local debounce = false
local tele1 = script.Parent.Tele1
script.Parent.MouseButton1Click:Connect(function()
if debounce == false then debounce = true
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local humanoidrootpart = char.HumanoidRootPart
humanoidrootpart.CFrame = tele1.CFrame
wait(1)
debounce = false
end
end)