I put a textbutton inside a surface gui which is in a part. I put a localscript and a simple code:
local Gui = script.Parent.Parent local Button = script.Parent Button.MouseButton1Click:Connect(function() print(1) end)
it doesn't print anything, or any error til I change a localscript into a script with the same code. This time, it prints 1 and I started to feel something's wrong.
Moreover, I want to get the Local player, who click the button, can anyone help me? Thanks a lot!
Im not a scripting expert or anything, in fact, im a rookie, so this may not work. Instead of naming the function blank, you can name the function player, like the player who hit the button. Like I said, this may not work.
local Gui = script.Parent.Parent
local Button = script.Parent
Button.MouseButton1Click:Connect(function(player)
print(1)
--you can try this to see if it works
print(player.name)
end)
First of all, your printing is written wrong. you forgot the "
print("1")
You will also be needing a click detector, Your code is written for UI Buttons. StarterGui is what your writing, you need to change the script for SurfaceGui.
Second, you need to insert a click detector into the part. This code is the fixed code, please read to understand what I changed:
-- the local gui isn't needed.Button already connects it. local Button = script.Parent.Parent.Parent.ClickDetector -- or whatever the click detector is called. and connect this to the part. Parents might be 4 instead of 3 depending on what u put, they might even be 5, idk what your explorer looks like. Button.MouseButton1Click:Connect(function() print("1") end)
Make sure the surface gui is in the part as well. Like this:
Hope this helps! Enjoy!
This is similar to an error I made a few months back. LocalScripts will only run if it is a descendant of:
The player's character model
The player's PlayerGui
The player's PlayerScripts
The player's backpack, (Note: That with Tools, the tool is in the backpack when unequipped and on the player's character model when equipped)
Or if the localscript is part of replicated first
So what you could do is put the script in one of these, I'd say ReplicatedFirst is best but I'm still fairly new to scripting and keeping things like that tidy.
LocalScript:
local Button = (path to button ex. workspace.Part.SurfaceGui.TextButton) local player = game:GetService("Players").LocalPlayer Button.MouseButton1Click:Connect(function() print(1) end