Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

The local script can't work in the textbutton, while the script can. Can someone explain?

Asked by 4 years ago

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!

3 answers

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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)

0
This will not work as player is not connected correctly. He also didnt ask for player to be connected. RobloxGameingStudios 145 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

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:

  • Workspace
    • Part
      • ClickDetector
      • SurfaceGui
        • Frame or button or both
          • localscript inside button

Hope this helps! Enjoy!

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

This is similar to an error I made a few months back. LocalScripts will only run if it is a descendant of:

  1. The player's character model

  2. The player's PlayerGui

  3. The player's PlayerScripts

  4. 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)

  5. 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

Answer this question