How to fix my king of the hill system?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

I am trying to make my king of the hill system have the flags tween down when one 1 player is on it but when multiple come on it pauses the tween until one guy leaves again it resumes.

  1. What is the issue? Include screenshots / videos if possible!

The issue occurs when the second player joins the pad. It the output is printing “pausing tween” so it is reading that there are multiple people on it but the tween does not pause even though I call the pause method. I assume it is something to do with using runService but can not find any issue or errors.

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I have tried many different methods but they give me the same issue and can not figure out why it is working.

here is my code

local TweenService = game:GetService("TweenService")
local RunService = game:GetService("RunService")
local DataManager require(game:GetService("ServerScriptService"):WaitForChild("Modules").DataManager)

local FlagParts = workspace:WaitForChild("Flags/King Of The Hill").FlagParts
local FlagModels = workspace:WaitForChild("Flags/King Of The Hill").FlagModels

local FlagPositions = {	
Down = {Flag1 = Vector3.new(-54.348, 103.035, 298.019)};
Up = {Flag1 = Vector3.new(-54.348, 120.375, 298.019)};
}

local ZonePlus = require(game:GetService("ServerScriptService"):WaitForChild("Modules").ZonePlus)
local ZoneService = require(ZonePlus.ZoneService)

local PlayerInside = {Flag1 = false}
local NumOfPlayersTouching = {Flag1 = 0}

for _,part in pairs(FlagParts:GetChildren()) do
	
	local zone = ZoneService:createZone(part.Name.."Zone", part, 15)

	zone.playerAdded:Connect(function(player)

		NumOfPlayersTouching[part.Name] = NumOfPlayersTouching[part.Name] + 1

		if NumOfPlayersTouching[part.Name] > 1  then
			PlayerInside[part.Name] = false
		else
			PlayerInside[part.Name] = player.Name
		end
	end)
	
	zone.playerRemoving:Connect(function(player)
		NumOfPlayersTouching[part.Name] = NumOfPlayersTouching[part.Name] - 1

		if NumOfPlayersTouching[part.Name] > 1  then
			PlayerInside[part.Name] = false
		else
			PlayerInside[part.Name] = player.Name
		end
	end)

	zone:initLoop()
end 

RunService.Stepped:Connect(function()
	for _,part in pairs(FlagParts:GetChildren()) do
		local UIPart = FlagModels[part.Name].FlagModel.UIPart
		local tween = TweenService:Create(UIPart, TweenInfo.new(15), {Position = FlagPositions.Down[part.Name]})
		if PlayerInside[part.Name] ~= false then
			if part.CurrentLeader.Value ~= PlayerInside[part.Name] then	
				tween:Play()	

				part.CurrentLeader.Value = PlayerInside[part.Name]
				
				UIPart.UI1.Enabled = false
				UIPart.UI2.Enabled = false
			end	
		else
			print("pausing tween")
			tween:Pause()
		end	
		tween:Destroy()
	end
end)

1 Like

I think the problem lies in this line. Every RunService.Stepped you are creating a new tween. I believe that means when you try to pause the tween you started, you are actually pausing a new tween that isn’t even playing.

Thats what I thought to but after destroying the tween each time and then pausing it the tween still does not stop

I’m not sure if this will work since I can’t really test it myself, but here’s a go at a fix:

local TweenService = game:GetService("TweenService")
local RunService = game:GetService("RunService")
local DataManager require(game:GetService("ServerScriptService"):WaitForChild("Modules").DataManager)

local FlagParts = workspace:WaitForChild("Flags/King Of The Hill").FlagParts
local FlagModels = workspace:WaitForChild("Flags/King Of The Hill").FlagModels

local FlagPositions = {	
Down = {Flag1 = Vector3.new(-54.348, 103.035, 298.019)};
Up = {Flag1 = Vector3.new(-54.348, 120.375, 298.019)};
}

local ZonePlus = require(game:GetService("ServerScriptService"):WaitForChild("Modules").ZonePlus)
local ZoneService = require(ZonePlus.ZoneService)

local PlayerInside = {Flag1 = false}
local NumOfPlayersTouching = {Flag1 = 0}

for _,part in pairs(FlagParts:GetChildren()) do
	
	local zone = ZoneService:createZone(part.Name.."Zone", part, 15)

	zone.playerAdded:Connect(function(player)

		NumOfPlayersTouching[part.Name] = NumOfPlayersTouching[part.Name] + 1

		if NumOfPlayersTouching[part.Name] > 1  then
			PlayerInside[part.Name] = false
		else
			PlayerInside[part.Name] = player.Name
		end
	end)
	
	zone.playerRemoving:Connect(function(player)
		NumOfPlayersTouching[part.Name] = NumOfPlayersTouching[part.Name] - 1

		if NumOfPlayersTouching[part.Name] > 1  then
			PlayerInside[part.Name] = false
		else
			PlayerInside[part.Name] = player.Name
		end
	end)

	zone:initLoop()
end 

local tweens = {}

RunService.Stepped:Connect(function()
	for _,part in pairs(FlagParts:GetChildren()) do
		local UIPart = FlagModels[part.Name].FlagModel.UIPart
		local tween = TweenService:Create(UIPart, TweenInfo.new(15), {Position = FlagPositions.Down[part.Name]})
		if not tweens[UIPart] then tweens[UIPart] = tween end
		if PlayerInside[part.Name] ~= false then
			if part.CurrentLeader.Value ~= PlayerInside[part.Name] then	
				tweens[UIPart]:Play()	

				part.CurrentLeader.Value = PlayerInside[part.Name]
				
				UIPart.UI1.Enabled = false
				UIPart.UI2.Enabled = false
			end	
		else
			print("pausing tween")
			tweens[UIPart]:Pause()
		end	
	end
end)

May end up breaking, but hopefully it helps!

that solved it :grin: thank you very much