Function not Running

Hello!

I have got a problem in my script where my function doesn’t run with the character added part. If you know a solution without removing the part, please say!

Also I’m on a phone so I can’t access studio right now but I will later.


SellInventoryBTN.MouseButton1Click:Connect(function()
		player.CharacterAdded:Wait()
		print("Sell Inventory CLicked")
		
		local tool = player.Backpack:FindFirstChildWhichIsA("Tool")
		local tools = player.Backpack:GetChildren()
		RemoveSoldItem:FireServer(player, tool)
		print(#tools)
		for i, tool in pairs(tools) do
			if TotalPrice ~=0 then
				tool:Destroy()
				
			else
				for i, tool in ipairs(tools) do
					tool:Destroy()
				end
			end
			
		end
		
		player.leaderstats.Credits.Value += TotalPrice
		TotalPrice = 0

		
		
	end)

Why is it waiting for the character to be added?

You shouldn’t need to wait for the character since it has nothing to do with the UI button the user is interacting with.

In another script it needs to wait for the character to check if the tools in the player’s backpack is there or not.I don’t know why but when i remove it, it doesn’t check and gives me tools that weren’t in my inventory before I left the game.

I have made a previous post with the scripts so look in to that

https://devforum.roblox.com/t/how-to-identify-a-table-from-another-function/3479498/42

Doesnt fire until the character loads, so if the character is loaded when the function runs, it will wait until the player dies and respawns,

Instead you should do this:

local Character = player.Character
if not Character then
 Character = player.CharacterAdded:Wait()`
end

OR

local Character = player.Character or player.CharacterAdded:Wait()

(Wrote this on my phone, sorry if there are any mistakes)

I will try that once I can be on my pc which has Roblox studio!

1 Like