Ad
Log in to vote
0

[Solved] My script is not working how I expected?

Asked by 1 month ago
Edited 1 month 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:

--[[ Values ]]--
local canDash = true
local looping = true

--[[ Math ]]--

--[[ Services ]]--
local runService = game:GetService("RunService")
local userInputService = game:GetService("UserInputService")

--[[ Player Variables ]]--
local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local mouse = player:GetMouse()

--[[ Variables ]]--
local humanoidRootPart = character:WaitForChild("HumanoidRootPart")

--[[ Functions ]]--

--[[ Script ]]--
runService.RenderStepped:Connect(function()
    local mTarget = mouse.Target
    local humanoid = mTarget.Parent:FindFirstChild("Humanoid")
    local targetHumanoidRootPart = mTarget.Parent:FindFirstChild("HumanoidRootPart")

    if humanoid then
        if not targetHumanoidRootPart then return end

        local distance = (targetHumanoidRootPart.Position - humanoidRootPart.Position).Magnitude

        if mTarget.Parent.Name == player.Name then return end

        if (distance > 40) then canDash = false return end

        userInputService.InputBegan:Connect(function(input, gameProcessed)
            if input.KeyCode == Enum.KeyCode.E then
                print("E has been pressed")
                if canDash then
                    print("Dashed")
                    canDash = false
                    humanoidRootPart.CFrame = targetHumanoidRootPart.CFrame
                    wait(.3)
                    canDash = true
                end
            end
        end)
    end
end)

Any help is appreciated!

0
print distance after you set that variable and see if that is working as you intend cmgtotalyawesome 1233 — 1mo

1 answer

Log in to vote
0
Answered by 1 month ago

I fixed it. All I did was add a debounce after the print.

Ad

Answer this question