MouseButton1Click Only works in studio

Recently I noticed that MouseButton1Click literally doesn’t work at all in-game. I don’t understand the issue at all (I am unsure what thread to put this in but I think this is probably the right place, please move it if you want to).

This bug doesn’t happen as often, sometimes maybe.
This is the script that I am using:

wait(2)
local Player = game.Players.LocalPlayer
local Character = Player.Character
local Humanoid = Character:FindFirstChild("Humanoid")
local HumanoidRootPart = Character:FindFirstChild("HumanoidRootPart")

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UIS = game:GetService("UserInputService")
local GamePassService = game:GetService("MarketplaceService")
local TweenService = game:GetService("TweenService")
local PlayerGui = Player.PlayerGui

local Button = script.Parent

Button.MouseButton1Click:Connect(function()
	if Button then
		print("why no work")
		script.Parent.Text = "teleported"
	end
end) 

(i put a teleportservice awhile ago) but in this screenshot it clearly shows that it works perfectly fine in studio.

In game:

It completely refuses to work. The reason why I say this bug only happens sometimes is because my menu screen is in the ScreenGui and that works perfectly fine without any issues.

I don’t think you need this line:

if Button then

try removing it to see if it works now

3 Likes

Seems like you are trying to do a Client side action that then does a worldspace action.

So you click a client side screen gui, and it teleports you to the location you pick in your gui.

Have the buttons trigger client to server events. So you basically tell the server “Hey can you teleport me please?” It can then access the player:FindFirstChild(“HumanoidRootPart”) and then set the CFrame to the location where the player will teleport to.

You might want to debounce it too so people can’t just spam the button and send like 200 events to the server before the lag catches up and teleports the player.

I added that after, even when I removed it it was the same problem.

Could you send a sceeenshot of the ancestry of the LocalScript and debug with something like this:

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:FindFirstChild("Humanoid")
local HumanoidRootPart = Character:FindFirstChild("HumanoidRootPart")

print("Character: "..Character)
print("Humanoid: "..Humanoid)
print("Root: ".. HumanoidRootPart)

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UIS = game:GetService("UserInputService")
local GamePassService = game:GetService("MarketplaceService")
local TweenService = game:GetService("TweenService")
local PlayerGui = Player.PlayerGui

local Button = script.Parent
print("Button: "...Button)
print("Type: "..Button.ClassName)
print("Event: "..Button.MouseButton1Click)

Button.MouseButton1Click:Connect(function()
    print("Ran")
	if Button then
		print("why no work")
		script.Parent.Text = "teleported"
	end
end) 

I had remotes placed on the GUI but there is no point on putting a emote when the localscript is literally broken and won’t work

1 Like

I tested it, literally nothing prints.

Where is your script in your game? ( just for clarification)

Player > PlayerGui > LockOnUI > TextButton

i feel like i know a probable cause of your issue, adding some WaitForChilds or some overall “waiting” in your script might help, because a couple of things you call in your script might not have been “initialized”, before you try and refrence something else, here’s an example of what i mean:

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait() 

or even here

local PlayerGui = Player:WaitForChild("PlayerGui")

I appreciate your help so far, but I am gonna close this stupid thread that I made. I literally just had to put a wait(3) on top of my script and it worked. Sorry for wasting your time

That Might work however, i wouldn’t recommend doing that always, because not everyone’s computer loads at the same, The Client’s “timing” can have uncertainties , it is not a reliable way to go about things. I would recommend adding some WaitFors and such in your code nonetheless, but i would make sure to use them when you should and use them correctly

Also @colbert2677 i am sure you wouldn’t mind if i linked this thread: i find it to be useful and resourceful

1 Like

Could I ask what specific bit you’re having trouble on? Is there supposed to be text in the provided screenshot that is meant to change? To me, I’m getting confused because the source of error looks completely different from a nonfunctional button.

This thread isn’t stupid, it’s a good opportunity to learn about where you actually went wrong and how to properly rectify that. Waits at the beginning of your code are indicative of an anti-pattern called a code smell which you should be avoiding in production code.

Might be obvious but I’m asking for clarification’s sake so I don’t end up wrongly assuming anything.

1 Like