Need help understanding why my king of the hill script isn't working

I would like to preface this topic by stating that I’ve poked around the other topics with similar names but they seem to be taking an entirely different approach to this. I would also like to clarify that I am fairly new to scripting.

So the general goal of this script is to get the name of the player’s character when they’ve stepped on the part and thus have the text label state, (Character.Name…“is king of the hill”). I seem to have hit a snag though as it’s not doing anything and I have no error. I’ve poked around a bit in the API and I’m not sure what I’ve done wrong. I do believe that the solution is probably a simple one though.

local part = script.Parent
local debounce = false
local playersService = game:GetService("Players")
local players = playersService:GetPlayers()


part.Touched:Connect(function(hit)
	local hitParent = hit.Parent
	
	local humanoid = hitParent:FindFirstChildWhichIsA("Humanoid")
	if humanoid then
		if not debounce then
			
		debounce = true
		
			for i, v in pairs(players)do
				v.PlayerGui.ScreenGui.Frame.TextLabel.Text = (hit.Parent.Name.."is king of the hill")
			end
			wait(1)
			
			debounce = false
			
		end
	end
end)

This is the script which is inside the part, it is currently the only script inside the game.

Try this and see what Outputs you get?

local part = script.Parent
local debounce = false
local playersService = game:GetService("Players")
local players = playersService:GetPlayers()


part.Touched:Connect(function(hit)
    print("Part Touched")
	local hitParent = hit.Parent
	
	local humanoid = hitParent:FindFirstChildWhichIsA("Humanoid")
	if humanoid then
		if not debounce then
            print("Changing Debounce")
			
		    debounce = true
		
			for i, v in pairs(players)do
                print("Changing Player's GUI")
				v.PlayerGui.ScreenGui.Frame.TextLabel.Text = (hit.Parent.Name.."is king of the hill")
			end
			wait(1)
			
			debounce = false
			
		end
	end
end)

image

It hasn’t changed the text label, it just seems to have broken the debounce.

Put local players = playersService:GetPlayers() inside the function

1 Like

That worked, thank you so much!