[Solved] My script is not working how I expected?
Asked by
4 years ago Edited 4 years ago
Hey there! I am making a script where you can to the enemy the mouse is pointing at. The only problem however, is that you would still be able to dash, even when the mouse is off the enemy or you moved the specified 40 studs away from the enemy
Here is that script:
09 | local userInputService = game:GetService( "UserInputService" ) |
13 | local character = player.Character or player.CharacterAdded:Wait() |
14 | local mouse = player:GetMouse() |
23 | local mTarget = mouse.Target |
24 | local humanoid = mTarget.Parent:FindFirstChild( "Humanoid" ) |
25 | local targetHumanoidRootPart = mTarget.Parent:FindFirstChild( "HumanoidRootPart" ) |
28 | if not targetHumanoidRootPart then return end |
30 | local distance = (targetHumanoidRootPart.Position - humanoidRootPart.Position).Magnitude |
32 | if mTarget.Parent.Name = = player.Name then return end |
34 | if (distance > 40 ) then canDash = false return end |
36 | userInputService.InputBegan:Connect( function (input, gameProcessed) |
37 | if input.KeyCode = = Enum.KeyCode.E then |
38 | print ( "E has been pressed" ) |
42 | humanoidRootPart.CFrame = targetHumanoidRootPart.CFrame |
Any help is appreciated!