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