Attempt to connect failed: passed value not a function

Hello everyone, For some reason im getting this error in my console, And I don’t know why.

Tool = game.ReplicatedStorage.Paper
button = game.StarterGui.ScreenGui.TextButton
function onPlayerEntered(newPlayer)
		wait(.5)
	local stats = Instance.new("IntValue")
	stats.Name = "leaderstats"

	local score = Instance.new("IntValue")
	
	score.Name = "Money"
	score.Value = 0

	score.Parent = stats	
	stats.Parent = newPlayer



function OnClicked() 
	Tool.Equipped:Connect(function(mouse)
		Tool:Destroy()
			
		end)
    end 
end

button.MouseButton1Down:connect(OnClicked)
game.Players.ChildAdded:connect(onPlayerEntered)
	

Maybe try putting the OnClicked function outside of the onPlayerEntered function.

Also, the GUIs that are in the StarterGui are copied to the player’s PlayerGui.

Tool = game.ReplicatedStorage.Paper
button = game.Players.LocalPlayer:WaitForChild("PlayerGui"):WaitForChild("ScreenGui").TextButton
function onPlayerEntered(newPlayer)
	wait(.5)
	local stats = Instance.new("IntValue")
	stats.Name = "leaderstats"

	local score = Instance.new("IntValue")

	score.Name = "Money"
	score.Value = 0

	score.Parent = stats	
	stats.Parent = newPlayer
end

function OnClicked() 
	Tool.Equipped:Connect(function(mouse)
		Tool:Destroy()
	end)
end 

button.MouseButton1Down:connect(OnClicked)
game.Players.ChildAdded:connect(onPlayerEntered)

:connect is deprecated, please use :Connect

You forgot to put local Tool = ... and local Button = ...

It still isn’t working, Is there a problem with the script?

“Workspace.leaderstats:2: attempt to index nil with ‘WaitForChild’”

1 Like

The whole script is the problem, it’s messy, inefficient, and why are you making leaderstats in a localscript? Not to mention, the leaderstats is not even a folder, its an intvalue.

1 Like

In which line the error occurs.

1 Like

It dosen’t tell me but I believe line 2.

I am a begineer, And havent scripted in over a month, I apologize, And It isnt in a local script.

if ur intending to make leaderstats isnt leaderstats instance supposed to be a folder and not an intvalue?

edit:nvm just watch a tutorial

How should I make it into a folder?

The error occurs because Players.LocalPlayer can only be used in local scripts.

You need to create the leaderstats in a server script and detect when the player presses the button in a localscript.

Im making it all in 1 script so further down the line I will increase the value of money in the script once you click the button and destroy your tool

I dont know how to get a local script and a normal script to communicate with eachother

1 Like

I don’t recommend making everything in one script, especially not with this. Since some part of that script are supposed to be in a localscript and some a serverscript.

It is possible though, you could fire the MouseButton1CLick from the server if you have correct variables.

And, @westyboy609, you can get 2 scripts to communiate with each other by using RemoteEvents.

Firing would still require 2 scripts.

If you’re talking about the MouseButton1Click, then no. You could do for example Player.PlayerGui.ScreenGui.TextButton.MouseButton1Click:Connect(function() end) You wouldn’t have to though but I also don’t recommend doing everything on the server to be fair, some things are more performance friendly on the client.

No, the player would be clicking the button that is located in player gui not starter gui, also how would you get the player that clicked the button, you would need to loop through each player and connect the function, unless you fire a remoteevent from a localscript when the player clicked the button which is easier. and also you can’t get mousebutton1click from a server sided script, and I don’t see why you would do that, I see why you would fire from a local script but not from a server script.

You can’t fire anything from StarterGui anyways, since it’ll get cloned into PlayerGui as you said. You can get MouseButton1Click from the server. Try it out :), some people do it though.

I still don’t see why you would do that, it’s easier to fire from a localscript when the player clicks the button to the server and not the other way around. And you most definitely CAN fire from startergui

Yeah, I also don’t see why. But some people do :man_shrugging:. How can you fire from StarterGui if it’s in PlayerGui…?