Passed argument somehow ending up as a player when it is required to be a BasePart

I am not sure how or why, but when I run this code (this is inside a ServerScript located within ServerScriptService):

local serverStorage = game:GetService("ServerStorage")
local replicatedStorage = game:GetService("ReplicatedStorage")
local debris = game:GetService("Debris")

local buttonsFolder = game.Workspace["Main Room"].Buttons
local EUcount = serverStorage.ExpansionUnits

function buttonClickFunctionality(button)

	local name = button.ButtonName.Value
	local price = button.Price.Value
	local level = button.Level.Value
	local maxlevel = button.MaxLevel.Value
	
	
	
	if EUcount.Value < price then
		coroutine.create(function()
			local errorSound = Instance.new("Sound")
			errorSound.SoundId = "rbxassetid://1388726556"
			errorSound.Parent = button
			errorSound.RollOffMaxDistance = 50
			errorSound.Loaded:Wait()
			errorSound:Play()
			debris:AddItem(errorSound, errorSound.TimeLength)
		end)
	end
end


function buttonFunctionConnect(button)
	local cd = button:FindFirstChild("ClickDetector")
	if cd then
		button.ClickDetector.MouseClick:Connect(buttonClickFunctionality, button)
	else
		return
	end
end

for _,item in pairs(buttonsFolder:GetChildren()) do
	if item:IsA("BasePart") then
		buttonFunctionConnect(item)
	end
end

The script attempts to find the value of a ButtonName stringvalue inside of my player, instead of the value of the ButtonName within the button who’s clickdetector I triggered. Considering the Players service is not mentioned whatsoever within this script, and the Player is not a BasePart, I am confused as to why this even happens in the first place. I have searched the devforum (but considering I barely even know how to describe this error, I didn’t find anything of use) and looked through the code several times and am still stumped as to what the cause could be.

Screenshot of the error for context (first two lines aren’t relevant):

1 Like

Arguments after function to MouseClick don’t automatically pass to that function. Also, player who clicked the detector is the argument passed to the MouseClick callback so that should explain your error.

button.ClickDetector.MouseClick:Connect(function()
    buttonClickFunctionality(button)
end)
1 Like

Oh, rookie mistake. Haven’t scripted in a while so that’s something I forgot. Thank you!

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