How can I fix my King of The Hill system?

Hello! I’m working on a King of the Hill system that makes a boolvalue in a player set to true if they become king. I’m able to successfully change the value, but I’m still having a lot of issues. It won’t make the value false when the player leaves, sometimes it doesn’t change the value at all. I’m just very overloaded with problems and need lots of help for this. Any assistance in finding the solution is greatly appreciated!

part = script.Parent
part.Touched:Connect(function(Touched)
	local Player = game:GetService("Players"):GetPlayerFromCharacter(Touched.Parent)
	if Player ~= nil then
		wait(1)
		for _, IteratedPlayer in pairs(game:GetService("Players"):GetChildren()) do
			if IteratedPlayer == Player then
				Player.Koth.Value = true 
				script.Parent.KothInfo.Info.Frame.Title.Text = (Player.Name.." is king of the hill!")
			else
				script.Parent.KothInfo.Info.Frame.Title.Text = "No one is king of the hill!"
				IteratedPlayer.Koth.Value = false
			end
		end
	end
end)
2 Likes

Well there’s a lot of problems with this script and this isn’t the best way of doing it. Anyways…

local targetKing = nil
local king = false
local players = {}

part.Touched:Connect(function(hit)
   if hit.Parent:FindFirstChild("Humanoid") then --Much better too
      table.insert(hit.Parent,players)
      if not king then
         local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
         task.wait(1)
         king = true
         targetKing = player
         player.Koth.Value = true
         script.Parent.KothInfo.Info.Frame.Title.Text = player.Name.." is king of the hill!"
      end
   end
end)

part.TouchEnded:Connect(function()
   for _,player in pairs(players) then
      if targetKing == player then
         script.Parent.KothInfo.Info.Frame.Title.Text = "No one is king of the hill!"
         king = false
         targetKing = nil
      end
   end
end)

Sorry if this code doesn’t worry I rushed it.

3 Likes

Don’t worry! I fixed one thing but Im getting the error, “Workspace.Map.Koth.Zone.Script:8: invalid argument #1 to ‘insert’ (table expected, got Instance)” on line 8

part = script.Parent
local targetKing = nil
local king = false
local players = {}

part.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then --Much better too
		table.insert(hit.Parent,players)
		if not king then
			local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
			task.wait(1)
			king = true
			targetKing = player
			player.Koth.Value = true
			script.Parent.KothInfo.Info.Frame.Title.Text = player.Name.." is king of the hill!"
		end
	end
end)

part.TouchEnded:Connect(function()
	for _,player in pairs(players) do
	if targetKing == player then
		script.Parent.KothInfo.Info.Frame.Title.Text = "No one is king of the hill!"
		king = false
		targetKing = nil
	end
end
end)
2 Likes

Oh my bad silly mistake I’m pretty sure it’s the other way around if it is you might want to fix the other one on the bottom too.

table.insert(players,hit.Parent)
2 Likes

Yes it works now! But it still isn’t making the value false on death or leaving.

1 Like

I’ll create a file so you can see how a working one is.

1 Like

KingOfHill.rbxl (33.5 KB)

I tested this and it worked now you probably need a remote event to fire an change the text to “New King” or “No King.”

2 Likes

Got it to work! Couldn’t thank you more for your help.

1 Like