Script isn't working when tool is equipped

(this is a local script) required (tool required handle is off)

local  UIS = game:GetService("UserInputService")

local last = tick()
local isFlying = false
local Speed = 32

local Tool = script.Parent
local Character = script.Parent.Parent
local HumanoidPart = Character:WaitForChild("HumanoidRootPart")

Tool.Equipped:Connect(function()
    
local function flying(isFlying)
    if isFlying then
        local BodyV = Instance.new("BodyVelocity")
        local VectorV = Instance.new("VectorForce")
        local Force = 3500
        local Attachment = Instance.new("Attachment")
        VectorV.Attachment0 = Attachment
        wait(3)
        VectorV.Force = Vector3.new(0,math.sin(math.rad(60)),-math.cos(math.rad(60))).Unit*Force
        BodyV.MaxForce = Vector3.new(0,1,0) * 10000
        BodyV.Velocity = Vector3.new(0,0,0)
        BodyV.Name = "Vel"
        VectorV.Name = "VecV"
        Attachment.Name = "Atta"
        Attachment.Parent = HumanoidPart
        BodyV.Parent = HumanoidPart
        VectorV.Parent = HumanoidPart
    else
        local B = HumanoidPart:FindFirstChild("Vel")
        local V = HumanoidPart:FindFirstChild("VecV")
        local A = HumanoidPart:FindFirstChild("Atta")
        if B then B:Destroy() end
        if V then V:Destroy() end
        if A then A:Destroy() end
        end
    end

UIS.InputBegan:Connect(function(input,proccessed)
    if proccessed then return end
    if input.KeyCode == Enum.KeyCode.E then
        if (tick() - last) < 0.5 then
            isFlying = not isFlying
            flying(isFlying)
        elseif isFlying then
            local B = HumanoidPart:FindFirstChild("Vel")
            if B then B.Velocity = Vector3.new(0,1,0) * Speed end
        end
        last = tick()
    end
    if input.KeyCode == Enum.KeyCode.Q then
        if isFlying then
            local B = HumanoidPart:FindFirstChild("Vel")
            if B then B.Velocity = Vector3.new(0,-1,0) * Speed end
             end
        end
    end)
UIS.InputEnded:Connect(function(input,processed)
    if processed then return end
    if input.KeyCode == Enum.KeyCode.E or input.KeyCode == Enum.KeyCode.Q then
        if isFlying then
            local B = HumanoidPart:FindFirstChild("Vel")
            if B then B.Velocity = Vector3.new(0,0,0)end
            end
        end
    end)
end)

The reason it doesn’t work is that a LocalScript only runs when it is located somewhere directly related to the LocalPlayer, such as StarterGui, ReplicatedFirst, StarterPlayerScripts and StarterCharacterScripts.

In my other tool with a local script that one works (just a simple Print script)

When a tool is equipped, it is put in the character. Is that not directly related to the player @SkyStoleMyCookies? Huh?

1 Like

Do you think you know why it would be doing this Zom?

Could you tell me what you are trying to accomplish?

1 Like

if i press e quickly it will start the script itself (ill change it to space later on)

Pretty much will start the flying script we were talking about

i know you problem I believe. Its your input began.

1 Like

Local scripts work fine in StarterPack even if it’s the child of a tool. The problem is with your script itself, particularly this line.

local Character = script.Parent.Parent

“script.Parent.Parent” refers to the player’s backpack, not the player’s character. This is a simple fix though, you can easily replace that line with these two to get the character properly.

local Player = Tool.Parent.Parent
local Character = Player.Character or Player.CharacterAdded:Wait()

However, there are still more problems with the script. It’s very bad to make connections for InputBegan and InputEnded each time the tool is equipped, so instead you should use a boolean for whether or not the tool is equipped using the Equipped and Unequipped events. I’ve done that for you and fixed the character problem, however the flight itself seems to be buggy. Let me know if you have any questions or need any more help.

local  UIS = game:GetService("UserInputService")

local last = tick()
local isEquipped = false
local isFlying = false
local Speed = 32

local Tool = script.Parent
local Player = Tool.Parent.Parent
local Character = Player.Character or Player.CharacterAdded:Wait()
local HumanoidPart = Character:WaitForChild("HumanoidRootPart")

local function flying(isFlying)
	if isFlying then
		local BodyV = Instance.new("BodyVelocity")
		local VectorV = Instance.new("VectorForce")
		local Force = 3500
		local Attachment = Instance.new("Attachment")
		VectorV.Attachment0 = Attachment
		wait(3)
		VectorV.Force = Vector3.new(0,math.sin(math.rad(60)),-math.cos(math.rad(60))).Unit*Force
		BodyV.MaxForce = Vector3.new(0,1,0) * 10000
		BodyV.Velocity = Vector3.new(0,0,0)
		BodyV.Name = "Vel"
		VectorV.Name = "VecV"
		Attachment.Name = "Atta"
		Attachment.Parent = HumanoidPart
		BodyV.Parent = HumanoidPart
		VectorV.Parent = HumanoidPart
	else
		local B = HumanoidPart:FindFirstChild("Vel")
		local V = HumanoidPart:FindFirstChild("VecV")
		local A = HumanoidPart:FindFirstChild("Atta")
		if B then B:Destroy() end
		if V then V:Destroy() end
		if A then A:Destroy() end
	end
end

Tool.Equipped:Connect(function()
	isEquipped = true
end)

Tool.Unequipped:Connect(function()
	isEquipped = false
end)

UIS.InputBegan:Connect(function(input,proccessed)
	if proccessed or not isEquipped then return end
	if input.KeyCode == Enum.KeyCode.E then
		if (tick() - last) < 0.5 then
			isFlying = not isFlying
			flying(isFlying)
		elseif isFlying then
			local B = HumanoidPart:FindFirstChild("Vel")
			if B then B.Velocity = Vector3.new(0,1,0) * Speed end
		end
		last = tick()
	end
	if input.KeyCode == Enum.KeyCode.Q then
		if isFlying then
			local B = HumanoidPart:FindFirstChild("Vel")
			if B then B.Velocity = Vector3.new(0,-1,0) * Speed end
		end
	end
end)

UIS.InputEnded:Connect(function(input,processed)
	if processed then return end
	if input.KeyCode == Enum.KeyCode.E or input.KeyCode == Enum.KeyCode.Q then
		if isFlying then
			local B = HumanoidPart:FindFirstChild("Vel")
			if B then B.Velocity = Vector3.new(0,0,0)end
		end
	end
end)
1 Like

You check if processed = true, if it does, you end the script. The thing is, if its true, then you pressed the key. So, try removing that.

1 Like

Or try @sean21307 advice. He is probably correct.

1 Like

It worked thank you very much.

Ouuter was checking the “gameProcessed” argument for InputBegan. You normally do want to return if gameProcessed is true, because otherwise it will pick up on undesirable input such as typing in chat.

2 Likes

Oh, well it still works sometimes

Could i have an example? please.

An example of using the gameProcessed parameter? You used it properly before, it caused no problems in the script. Now the problems are just with the flight logic itself, but here’s an example of using input began properly if that’s what you meant.

local UserInputService = game:GetService("UserInputService")

UserInputService.InputBegan:Connect(function(input, gameProcessed)
	if gameProcessed then
		return -- if the gameProcessed argument is true, don't execute whatever happens in this block
	end
	
	-- code to execute on input began
end)

Here’s the InputBegan API reference if you’re interested in a better explanation.

1 Like

Ok thank you. You’ve Been very helpful.