Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why can't I pass this value to this function?

function unselect()
	local children = script.Parent:GetChildren()
	for i = 1,#children do
		if children[i].ClassName == "ImageButton" and children[i].Chosen.Value == true then
			print("Unselected " .. children[i].Name)
			children[i].Chosen.Value = false
			children[i].BorderSizePixel = 2
			children[i].BorderColor3 = Color3.fromRGB(35,35,35)
			children[i]:TweenSize(UDim2.new(0, 80, 0, 80), "In", "Quad", .2)
		end
	end
end

function pselect(pokeID)
	local clicked = pokeID.Parent.Parent
	script.Parent.PokemonID.Value = pokeID
	unselect()
	clicked.Chosen.Value = true
	clicked.BorderSizePixel = 3
	clicked.BorderColor3 = Color3.fromRGB(57,141,230)
	clicked:TweenSize(UDim2.new(0, 90, 0, 90), "Out", "Quad", .2)
	print(pokeID)
	print(clicked.NameLabel.Text)
end

script.Parent.P151.MouseButton1Click:connect(pselect(script.Parent.P151.PokemonID.Value))

There's a bit of extra code there, but I figured you need to see it all, just in case. The problem is occurring on line 15,

local clicked = pokeID.Parent.Parent

The error message I get is:

Players.pokemon_origin.PlayerGui.ScreenGui.PokemonList.LocalScript:19: attempt to index local 'pokeID' (a number value)

The value I'm passing is an integer, it's just 151. The error seems to be in the passing of the value, because it gives an error the first time my script tries to use the passed value (151). If I missed out any information, please ask. Thanks.

EDIT: It clipped off the last line of the code, here it is: script.Parent.P151.MouseButton1Click :connect(pselect(script.Parent.P151.PokemonID.Value))

1 answer

✓ Accepted answer

Firstly, you say that the value being passed is an integer, but your function appears to expect an object.

Secondly, to pass arguments to a function connected to an event, you need to enclose the call in an anonymous function, like this:

script.Parent.P151.MouseButton1Click:connect(function() pselect(script.Parent.P151.PokemonID.Value) end)

Your existing code would call pselect with those arguments, then take the return value (if any) and call connect with that return value, which is not what you want.

1 comment

0

Thank you so much, I recall getting errors that complained I didn't pass a function, but I had no idea how one would even go about passing a function. A+ for you, good sir. Thanks!

This discussion is preserved as it was. New answers, comments, and votes are closed.