my code:
workspace.buildgui.Value = 0 script.Parent.Parent.Enabled = false local SP = script.Parent local slideno = 0 local goleft = script.Parent.goleft local goright = script.Parent.goright local buildingsnames = {"hut","stone hut"} while true do wait(0.5) if game.Workspace.buildgui.Value == 1 then script.Parent.Parent.Enabled = true if slideno == 0 then SP.Dinfo.BackgroundTransparency = 1 SP.Dinfo.Text = "" SP.Dbuy.BackgroundTransparency = 1 SP.Dbuy.Text = "" SP.Einfo.BackgroundTransparency = 1 SP.Einfo.Text = "" SP.Ebuy.BackgroundTransparency = 1 SP.Ebuy.Text = "" SP.Finfo.BackgroundTransparency = 1 SP.Finfo.Text = "" SP.Fbuy.BackgroundTransparency = 1 SP.Fbuy.Text = "" SP.Ginfo.BackgroundTransparency = 1 SP.Ginfo.Text = "" SP.Gbuy.BackgroundTransparency = 1 SP.Gbuy.Text = "" SP.Hinfo.BackgroundTransparency = 1 SP.Hinfo.Text = "" SP.Hbuy.BackgroundTransparency = 1 SP.Hbuy.Text = "" SP.Ainfo.BackgroundTransparency = 0 SP.Ainfo.Text = "info" SP.Abuy.BackgroundTransparency = 0 SP.Abuy.Text = "buy" SP.Binfo.BackgroundTransparency = 0 SP.Binfo.Text = "info" SP.Bbuy.BackgroundTransparency = 0 SP.Bbuy.Text = "buy" SP.Cinfo.BackgroundTransparency = 1 SP.Cinfo.Text = "" SP.Cbuy.BackgroundTransparency = 1 SP.Cbuy.Text = "" SP.Cimage.Visible = false SP.Dimage.Visible = false SP.Eimage.Visible = false SP.Fimage.Visible = false SP.Gimage.Visible = false SP.Himage.Visible = false SP.Aimage.Visible = true SP.Bimage.Visible = true function onclickbuy(letter) if letter == "A" then print("bfhsfhsljchl;s") if workspace.hosuesbrought.A.Value == 0 then game.ReplicatedStorage.Abuy.Parent = workspace workspace.hosuesbrought.A.Value = 1 end end end script.Parent.Abuy.MouseButton1Click:Connect(onclickbuy("A")) end end if workspace.buildgui.Value == 0 then SP.Parent.Enabled = false end end
the bit wrong is this:
function onclickbuy(letter) if letter == "A" then print("bfhsfhsljchl;s") if workspace.hosuesbrought.A.Value == 0 then game.ReplicatedStorage.Abuy.Parent = workspace workspace.hosuesbrought.A.Value = 1 end end end script.Parent.Abuy.MouseButton1Click:Connect(onclickbuy("A"))
when i run this happens 16:25:11.648 - Attempt to connect failed: Passed value is not a function 16:25:11.649 - Stack Begin 16:25:11.650 - Script 'Players.codingMASTER398.PlayerGui.ScreenGui.Frame.LocalScript', Line 62 16:25:11.650 - Stack End bfhsfhsljchl;s 16:25:12.164 - Attempt to connect failed: Passed value is not a function 16:25:12.164 - Stack Begin 16:25:12.165 - Script 'Players.codingMASTER398.PlayerGui.ScreenGui.Frame.LocalScript', Line 62 16:25:12.165 - Stack End bfhsfhsljchl;s 16:25:12.700 - Attempt to connect failed: Passed value is not a function 16:25:12.701 - Stack Begin 16:25:12.702 - Script 'Players.codingMASTER398.PlayerGui.ScreenGui.Frame.LocalScript', Line 62 16:25:12.702 - Stack End bfhsfhsljchl;s 16:25:13.214 - Attempt to connect failed: Passed value is not a function 16:25:13.214 - Stack Begin 16:25:13.215 - Script 'Players.codingMASTER398.PlayerGui.ScreenGui.Frame.LocalScript', Line 62 16:25:13.215 - Stack End 16:25:14.150 - Disconnect from 127.0.0.1|52506 help plzzzzzzzzzz
Remember that functions are first class values; table fields can contain references to them, variables can contain references to them, they can be returned by functions and can be passed around as arguments as well. So pass it to :Connect
! A function call is a statement and an expression. A function call evaluates to the functions return values if used as expression.
The issue is that you're immediately calling onclickbuy
and passing the return value (which there is none) to :Connect
. So it gets nil or nothing at all.
script.Parent.Abuy.MouseButton1Click:Connect(onclickbuy)
That will fix the error, but there's one more issue: MouseButton1Click
listeners get no arguments! letter
will always be nil. If you want to still pass "A"
as an argument, you can use a HOF (higher order function). A Higher Order Function is a function that returns a function and/or can take a function as an argument. An example of one is pcall
since it takes a function to call in protected mode
local function onclickbuy(letter) return function() if letter == "A" then print("bfhsfhsljchl;s") if workspace.hosuesbrought.A.Value == 0 then game.ReplicatedStorage.Abuy.Parent = workspace workspace.hosuesbrought.A.Value = 1 end end end end script.Parent.Abuy.MouseButton1Click:Connect(onclickbuy("A"))
And this works! The expression onclickbuy("A")
evaluates to a function.