Hey there, I have been working on a Flee the Facility inspired game for a few days now and got stuck at my generator repairing script. The generator repairing works similarly to Flee the Facility’s computer hacking system.
What would be the best way to detect, if the player went away from the generator?
Any help is appreciated!
script.Parent.hitbox.Interact.Triggered:Connect(function(plr)
if script.Parent.Percent.Value == 100 then
else
local timeLeft = math.random(1,10)
script.Parent.hitbox.Interact.Enabled = false
script.Parent.Status.Value = "WORKING"
repeat
wait(1)
timeLeft = timeLeft - 1
if script.Parent.Percent.Value >= 100 then
script.Parent.Percent.Value = 100
break
else
script.Parent.Percent.Value = script.Parent.Percent.Value + 2
end
until timeLeft == 0
I advise to use magnitude to find distance between two positions (pos1 - pos2).Magnitude
local function GetCharFromPlr(plr)
for i,v in ipairs(workspace:GetChildren()) do
if game:GetService("Players"):GetPlayerFromCharacter(v) == plr then
return v
end
end
return nil
end
local HowFarCanPlayerGoInStuds = 5
script.Parent.hitbox.Interact.Triggered:Connect(function(plr)
local char = GetCharFromPlr(plr)
local humrp = char:FindFirstChild("HumanoidRootPart")
if script.Parent.Percent.Value == 100 then
elseif (humrp.Position - script.Parent.hitbox.Position).Magnitude < HowFarCanPlayerGoInStuds then -- character must be atleast 5 studs away from hitbox
local timeLeft = math.random(1,10)
script.Parent.hitbox.Interact.Enabled = false
script.Parent.Status.Value = "WORKING"
repeat
wait(1)
timeLeft = timeLeft - 1
if script.Parent.Percent.Value >= 100 then
script.Parent.Percent.Value = 100
break
elseif (humrp.Position - script.Parent.hitbox.Position).Magnitude < HowFarCanPlayerGoInStuds then
script.Parent.Percent.Value = script.Parent.Percent.Value + 2
else
-- do here something in case your character leaved that place
break
end
until timeLeft == 0
end
end)
Also you can check if player’s humanoid health is greater than zero.
local function GetCharFromPlr(plr)
for i,v in ipairs(workspace:GetChildren()) do
if game:GetService("Players"):GetPlayerFromCharacter(v) == plr then
return v
end
end
return nil
end
local HowFarCanPlayerGoInStuds = 5
script.Parent.hitbox.Interact.Triggered:Connect(function(plr)
local char = GetCharFromPlr(plr)
local humrp = char:FindFirstChild("HumanoidRootPart")
local hum = char:WaitForChild("Humanoid")
if script.Parent.Percent.Value == 100 then
elseif (humrp.Position - script.Parent.hitbox.Position).Magnitude < HowFarCanPlayerGoInStuds and hum.Health > 0 then -- character must be atleast 5 studs away from hitbox
local timeLeft = math.random(1,10)
script.Parent.hitbox.Interact.Enabled = false
script.Parent.Status.Value = "WORKING"
repeat
wait(1)
timeLeft = timeLeft - 1
if script.Parent.Percent.Value >= 100 then
script.Parent.Percent.Value = 100
break
elseif (humrp.Position - script.Parent.hitbox.Position).Magnitude < HowFarCanPlayerGoInStuds and hum.Health > 0 then
script.Parent.Percent.Value = script.Parent.Percent.Value + 2
else
-- do here something in case your character leaved that place
break
end
until timeLeft == 0
end
end)