Distance kill now working properly?

Heyo! I was working on a “who dunnit” script for my killing game, however , I’ve reached an obstacle in the code and I cannot figure out how to fix it. Basically, what I want , is that if the killer is 10 studs away from the prey, a button pops up suggesting to kill them. This messes up multiple times , sometimes not even appearing at all when close to someone, and I’ve given up tbh.

So that’s why I’m here, this is the code I’m using , and any help would be amazing! I left comments in the code about a couple things.

if Player == Variable1 then -- Variable1 is just the murderer's username
			local INTERVAL = .2

			local KillCooldown  = 20

			local nextStep = tick() + INTERVAL
			game:GetService("RunService").Heartbeat:Connect(function(dt)
				
				if (tick() >= nextStep) then
					
					
					--nextStep = nextStep + INTERVAL
					if KillCooldown ~= 0 then
						Player.PlayerGui.Dialog.Kill.Visible = true
						Player.PlayerGui.Dialog.Kill.Text = KillCooldown
						KillCooldown = KillCooldown - 1
						nextStep = nextStep + 1
					else
						nextStep = nextStep + INTERVAL
						for unused, otherPlayer in pairs (Players) do
							if otherPlayer.Name ~= Player.Name then
								local distance = (Player.Character.HumanoidRootPart.Position - otherPlayer.Character.HumanoidRootPart.Position).magnitude
								-- need to find the closest, not just whoever is 10 away, because I think thats what breaks it
								if distance < 10 and KillCooldown <= 0 and otherPlayer.Character.Humanoid.Health ~= 0 then
									
									print(Player,otherPlayer) -- no button occuring sometimes
								

									Player.PlayerGui.Dialog.Kill.Text = "Kill ".. otherPlayer.Character:FindFirstChildOfClass("HumanoidDescription").Name
									Player.PlayerGui.Dialog.Kill.Visible = true
									
									Player.PlayerGui.Dialog.Kill.MouseButton1Click:Connect(function()
										RE:FireServer("Murdered",otherPlayer)
										KillCooldown = 20
									end)
								else

									Player.PlayerGui.Dialog.Kill.Visible = false

							end


						end

There is a lot of strange choices going on in this code that is most likely contributing to many logical blunders. There seems to be no real reason to be using “Heartbeat” and you should either be using “event-driven” code based on movement or simply:

while (wait(0.2)) do

end

Your code can be heavily simplified to

local isKillerNearAPlayer()
	for _, player in pairs(game.Players:GetPlayer()) do
		if (player:DistanceFromCharacter(game.Players.LocalPlayer.Character.PrimaryPart.Position) <= 10) then
			return player;
		end
	end
end

local lastKillTime = 0; 

while (wait(0.2)) do
	if (Player == Variable1) then
		if (tick() - lastKillTime > 20) then -- 20 seconds have passed
			local playerNearBy = isKillerNearPlayer()
			if (playerNearBy) then
				Player.PlayerGui.Dialog.Kill.Text = ("Kill %s"):format(playerNearBy.Name)
				Player.PlayerGui.Dialog.Kill.Visible = true
				-- etc
			end
		end
	end
end
1 Like

Not 100% sure on why I used heartbeat either, this is some pretty old code that I just got back into but looking at it now I could definitely just use a while loop.

also did not even know :DistanceFromCharacter was a thing! I’ll format my code real quick to adjust this, if I have any further questions , I’ll reach out, if not, I’ll give ya the solution! Thanks!