Part counter UI not working

local UIS = game:GetService("UserInputService")
PartCounter = 0
KeyPressed = false

UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.E then
		KeyPressed = true
		while KeyPressed == true do
			wait()
			local part = Instance.new("Part")
			part.Position = Vector3.new(-12.078, 36.522, 34.601)
			part.Size = Vector3.new(2,2,2)
			part.Parent = game.Workspace
			PartCounter = PartCounter + 1
			game.StarterGui.PartCounterUI.Number.Text = PartCounter
		end
	end
end)

UIS.InputEnded:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.Q then
		KeyPressed = false
	end
end)

so basically i made a UI to show the players how many parts they have spawned, i can say print(PartCounter) but then they wont be able to see how many parts they have spawned so idk what to do.

1 Like

You should be changing the text in the player’s playergui not startergui

1 Like

bruh moment for me there, let me try.

1 Like

You can’t use game.StarterGui.

There is 2 ways to fix this, I’m going to show you the easier way.

Replace the quoted part with:

for i,v in pairs(game.Players:GetPlayers()) do
    v.PlayerGui.PartCounterUI.Number.Text = PartCounter
end

The other way uses a RemoteEvent.

I’m not sure if your script is a LocalScript or a script, but I assume a normal script.

2 Likes

that worked, but for some reason now i have to manually press the button to spawn in part. but thanks for the help, i will try to fix the other problem on my own.

1 Like