✓ Accepted answer
So this code gets the closest computer then you can move to the computer .
Edit I added the new code follow the Instructions
Note1
This won't work on FE but you can make it work on FE by adding remote events.
Instructions (Important follow carefully)
1.So all the model computers should be in a folder called Computers
2.Put this script in game.StarterPlayer.StarterPlayerScripts
3.So every computer model should have a primary part note very important the primary part is the part the character will go to so put it just infront of the pc with anchored on and cancollide off and the part should be moved upward "like to the y position of the character.
4.note very important the the part where the player looks at like example the computer screen should be name Part i added comments to where that is code the CFrame look vector.
Read more https://wiki.roblox.com/index.php?title=Remote_Functions_%26_Events
You can modify it
Code is tested.
local player
local character
local isHacking = true
function waitForCharcter()
while true do
player = game.Players.LocalPlayer
if player.Character ~= nil then
character = player.Character.Humanoid
print(player.Character.Name)
print("isn nil")
return
end
wait(1)
end
end
waitForCharcter()
local allComputers = game.Workspace.Computers:GetChildren()
local radius = 10
local Animation = Instance.new("Animation")
Animation.AnimationId = "http://www.roblox.com/asset/?id=1456127401"
local Hacking = character:LoadAnimation(Animation)
local chosenIndex = nil
local chosenComputer = nil
local computerIndex = 1
function findClosestComputer(character)
for i = 2,#allComputers do
local distance = player:DistanceFromCharacter(allComputers[computerIndex].PrimaryPart.Position)
local nextDistance = player:DistanceFromCharacter(allComputers[i].PrimaryPart.Position)
if distance < nextDistance then
chosenIndex = computerIndex
chosenComputer = allComputers[chosenIndex]
else
chosenIndex = i
chosenComputer = allComputers[i]
computerIndex = i
end
end
return chosenComputer
end
function onKeyPress(inputObject, gameProcessedEvent)
if inputObject.KeyCode == Enum.KeyCode.E then
local closestComputerPos = findClosestComputer(character)
if player:DistanceFromCharacter(closestComputerPos.PrimaryPart.Position) <= radius then
character:MoveTo(closestComputerPos.PrimaryPart.Position)
character.MoveToFinished:wait()
if character.RigType == Enum.HumanoidRigType.R15 then
character.Parent.LowerTorso.CFrame = CFrame.new(closestComputerPos.PrimaryPart.Position,closestComputerPos.Part.Position)
else
character.Parent.Torso.CFrame = CFrame.new(closestComputerPos.PrimaryPart.Position,closestComputerPos.Part.Position)
end
wait(1)
isHacking = true
while isHacking ~= false do
print("playing")
wait(Hacking.Length)
Hacking:play()
end
Hacking:Stop()
end
end
end
character.Running:connect(function(speed)
if speed > 0 then
isHacking = false
end
end)
game:GetService("UserInputService").InputBegan:connect(onKeyPress)
6 comments
I am checking it out now do you teleport the character to the computer or only let him walk too there
And do you want to find the closest computer to the player?
Or wait since the radius is the distance that acounts for the closest.
Please don't mind my code. They are just a example. my question is: how do i get two or up with one local?
Well i wrote you new working code. and tested will post now
Ok thanks!