Tool script not working when cloned in to player backpack

I am trying to make my gun local script work when the player buys it or when it is picked up. It does not print anything. The script only works when its in Starterpack for some reason. I tried putting some of the locals into the tool.Equipped function but that didn’t work. I am confused.

local player = game.Players:WaitForChild("LocalPlayer")
local mouse = player:GetMouse()
local tool = script.Parent
local character = player.CharacterAdded:Connect(function(character)
local humanoid = character:WaitForChild("Humanoid")
local humanoidrootpart = character:WaitForChild("HumanoidRootPart")
local canaim = false
local playerGui = player:WaitForChild("PlayerGui")
local taskgui = playerGui:WaitForChild("Task")
local canaim2 = taskgui:WaitForChild("Val")
local mb2 = false
local mb1 = false
local anim = script.Animation

tool.Equipped:Connect(function()
	local Character = tool.Parent
	local Humanoid = Character.Humanoid
	local PlayAnim = Humanoid.Animator:LoadAnimation(anim)
	canaim = true
	canaim2.Value = true
	print("can aim should be true")
	mouse.Button2Up:Connect(function()
		mb2 = false
		if mb1 == false then
			PlayAnim:Stop()

		end
	end)
	mouse.Button1Down:Connect(function()
			mb1 = true
			if canaim == true then
				PlayAnim:Play()
			end
end)
	mouse.Button1Up:Connect(function()
		mb1 = false
		if mb2 == false then
			if mb2 == false then
				if mb1 == false then
					PlayAnim:Stop()
				end
			end
		end
	end)

	mouse.Button2Down:Connect(function()
		mb2 = true
		if canaim == true then
			PlayAnim:Play()
		end
		end)
end)

	tool.Unequipped:Connect(function()
		local Character = tool.Parent
		local Humanoid = Character.Humanoid
		local PlayAnim = Humanoid.Animator:LoadAnimation(anim)
		canaim = false
		canaim2.Value = false
		PlayAnim:Stop()
end)
end)

You don’t need to use a function for the character variable, you can just simply do:

local character = player.Character or player.CharacterAdded:Wait()

Also, what kind of script is it? Local or Server?

2 Likes

It is a local script inside of a tool and the tool is in serverstorage.

Are there any infinite yield warnings in your WaitForChild() variables? If not, add another print() statement outside your Events to make sure the script fully works, also it looks like you put the Unequipped Event inside the Equipped Event as well…? Put that outside as its own separate Event

1 Like

Thanks, I had to remove all the Waitforchild’s and put the animation local into the Tool.Equipped function.

1 Like