Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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!

1 comment

0

print distance after you set that variable and see if that is working as you intend

1 answer

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

This discussion is preserved as it was. New answers, comments, and votes are closed.