SurfaceGUI TextButton MouseButton1Down not firing

I don’t understand whats wrong with it.

My GUI is in StarterGui, the MouseButton1Down event is recieved from a LocalScript and passed to the server, and all the elements of the GUI have Active enabled. I have no idea what’s not working.

What i have.rbxm (16.5 KB)

Nothing is being printed or ran when the button is clicked

1 Like

Can you show a screenshot of the layout in studio, and also send your code?

I attached the RBXM file in the top post

image
image

-- Local Script
local Collect = script.Parent.Collect
local Buy = script.Parent.Buy

Collect.MouseButton1Down:Connect(function()
	print("hi")
	script.Parent.RemoteEvent:FireServer("collect")
end)
Buy.MouseButton1Down:Connect(function()
	print("hi")
	script.Parent.RemoteEvent:FireServer("buy")
end)
-- Server script
script.Parent.RemoteEvent.OnServerEvent:Connect(function(_, t)
	if t=="collect" then
		-- code
	elseif t=="buy" then
		-- code
	end
end)

Which button isn’t working? There’s 2 on your model.

Both of them. . . . . . . . . . .

Your local script isn’t running at all.
I put a print statement right at the top and it didn’t print.

It may be the location of the scripts.

Unfortunately I don’t know why your scripts aren’t running.

It may be something to do with the screengui, as I have tried placing individual scripts inside the buttons that fire when the button is clicked, but nothing happens.

DId you parent the GUI to StarterGui?
When i ran it is seemed to work.

No, I just left it in the model you put.

Right, i meant to say to parent the GUI to the StarterGUI and leave the generator where it is. If you try that does it work?

Parented it to StarterGui and changed these lines

Buy.MouseButton1Down:Connect(function()
Collect.MouseButton1Down:Connect(function()

to

Buy.MouseButton1Click:Connect(function()
Collect.MouseButton1Click:Connect(function()

Your scripts run like this where they are in the model.

Does that make it work? I tried changing it to that and it still didnt work.

It worked for me.
The GUI in starter gui, just changing those two lines.

Could you send the whole LocalScript and ServerScript code you have now?

local script code

local Collect = script.Parent.Collect
local Buy = script.Parent.Buy

Collect.MouseButton1Click:Connect(function()
	print("hi")
	script.Parent.RemoteEvent:FireServer("collect")
end)
Buy.MouseButton1Click:Connect(function()
	print("hi")
	script.Parent.RemoteEvent:FireServer("buy")
end)

server script code

local GUI = script.Parent

local Bar = GUI.Bar
local Fill = Bar.Fill

local Buy = GUI.Buy
local Collect = GUI.Collect

local Player
repeat
	task.wait(1)
	Player = GUI.Parent.Parent
until Player.ClassName == "Player"

local CashBank = 0
local Rungs = {}

local Tween_Info = {
	TweenInfo = TweenInfo.new(0.75, Enum.EasingStyle.Linear);
	Goal = {Size = UDim2.new(1,0,1,0)};
}
local Tween = game:GetService("TweenService"):Create(Fill, Tween_Info.TweenInfo, Tween_Info.Goal);

local Screen = GUI.Adornee
local Map = Screen.Parent.Parent

local RungModel = game.ServerStorage.GameData["Tycoon Battle"].Rung
local Origin = Map.RungOrigin.Position
local RungOffset = 3 -- size of the rungs
local function CreateNewRung()
	local Number = #Rungs + 1
	local Postion = Vector3.new(Origin.X, (Origin.Y-RungOffset)*(Number*RungOffset), Origin.Z)
	local Rung = RungModel:Clone()
	Rung:PivotTo(CFrame.new(Postion))
	Rung.Parent = Map
	table.insert(Rungs, Rung)
end

script.Parent.RemoteEvent.OnServerEvent:Connect(function(_, t)
	if t=="collect" then
		local Cash = tonumber(string.sub(Collect.Text, 9, -6))
		print(Cash, string.sub(Collect.Text, 9, -6))
		if Cash then
			CashBank = 0
			Collect.Text = "Collect "..tostring(CashBank).." Cash"
			Player.leaderstats.Cash.Value += 1
		else
			print(Player,"cash is nil")
		end
	elseif t=="buy" then
		local PlayerCash = Player.leaderstats.Cash.Value
		if PlayerCash >= 10 then
			Player.leaderstats.Cash.Value -= 10
			CreateNewRung()
		end
	end
end)

task.defer(function()
	while true do
		Tween:Play()
		Tween.Completed:Wait()
		Fill.Size = UDim2.new(0,0,1,0)

		CashBank += 1
		Collect.Text = "Collect "..tostring(CashBank).." Cash"
	end
end)

I dont understand why your one works but mine doesn’t. I changed it to exactly what you did and yet it still doesnt work.

I’m not sure, but they worked for me.

For some reason, my solution is setting the AlwaysOnTop value of the screen gui to true. I have not changed anything except this. I wonder why this is the behavior.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.