Tool.Equipped Is Not Working

  1. What do you want to achieve? Keep it simple and clear!
    i just want, for the GUI To appear when the player is in mobile.

  2. What is the issue? Include screenshots / videos if possible!
    Thing is, the first time i tried i thought it would work, it did not. i thought it would be a problem within “SetDevicePref” but i dont know. i added print statements inside SetDevicePref and Tool.Equipped but they arent printing. now im just terribly confused.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    i tried Firing SetDevicePref for when Mousebutton2 is held, to see if it would print. still a no. i tried searching through out the Devforum but alot of them i already tried and or not a error to my code.

For Some reason, the equip animation and setting Equipped to true still works. but setting it’s parent and firing SetDevicePref doesnt work.
i also removed alot of code just to see if they could be the source of the problem. turns out they werent. and lost a bunch of code.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

local Tool = script.Parent
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")
local TweenService = game.TweenService
local Cam = workspace.CurrentCamera

local servergun = Tool:WaitForChild("servergun")
local Cooldown = servergun.ShotCooldown

local Shoot = servergun:WaitForChild(Tool.Name .. "shoot")
local Reload = servergun:WaitForChild(Tool.Name .. "reload")
local Ammo = servergun:WaitForChild("Ammo")

local PlayerGUI = player.PlayerGui

local GUI
local Crosshair = PlayerGUI:WaitForChild("CrosshairGUI"):WaitForChild("Center")
local ShootAnimation = script:WaitForChild("Shoot")
local equipAnimation = script:WaitForChild("Equip")
local ReloadAnimation = script:WaitForChild("Reload")
local holdAimAnimation = script:WaitForChild("Hold")

local PlayerModule

local shootTrack = hum:LoadAnimation(ShootAnimation)
local equipTrack = hum:LoadAnimation(equipAnimation)
local ReloadTrack = hum:LoadAnimation(ReloadAnimation)
local holdAimTrack = hum:LoadAnimation(holdAimAnimation)

local TweenZoom = TweenService:Create(Cam, TweenInfo.new(0.5, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut), {FieldOfView = 30})
local TweenZoomOut = TweenService:Create(Cam, TweenInfo.new(0.5, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut), {FieldOfView = 70})

local mouse = player:GetMouse()
local UIS = game.UserInputService

local Equipped = false
local Aim = false
local CanShoot = true
local reloading = false


local function SetGripProperties(position, orientation)
		if Tool:IsA("Tool") then
		Tool.Grip = CFrame.new(position) * CFrame.Angles(math.rad(orientation.x), math.rad(orientation.y), math.rad(orientation.z))
	end
end

local function SetDevicePref()
	print("IsSettings")
	if not UIS.TouchEnabled and (UIS.KeyboardEnabled or UIS.MouseEnabled) then
		print("IsDesktop")
		UIS.MouseBehavior = Enum.MouseBehavior.LockCenter
		PlayerModule = require(player:WaitForChild("PlayerScripts", math.huge):WaitForChild("PlayerModule"))
	elseif UIS.TouchEnabled and not UIS.KeyboardEnabled and not UIS.MouseEnabled then
		print("IsMobile")
		GUI = game.ReplicatedStorage:WaitForChild("GunGUI"):Clone()
		GUI.Parent = PlayerGUI
	end
end

Tool.Equipped:Connect(function()
	script.Parent = player
	
	
	print("Equipped")
	SetDevicePref()
	equipTrack:Play()
	Equipped = true
	
end)

local function CameraFOV()
	if PlayerModule then
		PlayerModule:ToggleShiftLock(false, true, true)
	end
	
	TweenZoom:Play()
	mouse.Icon = "rbxassetid://15722065980"
	print("CameraChange")
end

local function ResetCameraFOV()
	mouse.Icon = "rbxasset://SystemCursors/Arrow"
	TweenZoomOut:Play()
	print("CameraChange")
end

local function ReloadFunction()
	reloading = true
	ReloadTrack:Play()
	Reload:FireServer()
	ResetCameraFOV()
	if UIS.KeyboardEnabled then
		UIS.MouseBehavior = Enum.MouseBehavior.LockCenter
	end
	Aim = false
	holdAimTrack:Stop()
	equipTrack:Play()
	PlayerModule:ToggleShiftLock(true, true, true)
	ReloadTrack.Stopped:Wait()
	reloading = false
end

UIS.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton2 then
		if Equipped then
			CameraFOV()
			if UIS.KeyboardEnabled then
				UIS.MouseBehavior = Enum.MouseBehavior.LockCenter
			end
			PlayerModule:ToggleShiftLock(false, true, true)
			hum.WalkSpeed = 16
			Aim = true
			SetGripProperties(Vector3.new(0, 0, 0), Vector3.new(0, 6, 0))
			equipTrack:Stop()
			holdAimTrack:Play()
		end
	end
end)

UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.R then
		print("pressed Reload Key")
		if Equipped then
			if not reloading then
				print("Reloading")
				ReloadFunction()
			end
		end
	end
end)

Ammo:GetPropertyChangedSignal("Value"):Connect(function()
	if Ammo.Value == 0 and not reloading then
		ReloadFunction()
	end
end)

UIS.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		if Equipped then
			if Aim then
				if CanShoot then
					print("shot")
					CanShoot = false

					Shoot:FireServer(Tool.Handle.CFrame.Position, mouse.Hit.p, char)
					shootTrack:Play()

					wait(Cooldown.Value)
					CanShoot = true
				end
			end
		end
	end
end)

UIS.InputEnded:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton2 then
		if Equipped then
			ResetCameraFOV()
			Aim = false
			if UIS.KeyboardEnabled then
				UIS.MouseBehavior = Enum.MouseBehavior.LockCenter
			end

			holdAimTrack:Stop()
			equipTrack:Play()
			PlayerModule:ToggleShiftLock(true, true, true)
		end
	end
end)

hum:GetPropertyChangedSignal("WalkSpeed"):Connect(function()
	if hum.WalkSpeed == 25 then
		if Aim and Equipped then
			ResetCameraFOV()
			Aim = false
			holdAimTrack:Stop()
			equipTrack:Play()
			PlayerModule:ToggleShiftLock(true, true, true)
		end
	end
end)

Tool.Unequipped:Connect(function()
	Aim = false
	UIS.MouseBehavior = Enum.MouseBehavior.Default
	ResetCameraFOV()
	Equipped = false
	holdAimTrack:Stop()
	equipTrack:Stop()
	PlayerModule:ToggleShiftLock(true, true, true)
	script.Parent = Tool
	if GUI then
		GUI:Destroy()
	end
end)

Hey @iiRillz! How are you?

Where is this script located? Is it in StarterPlayer? Furthermore, are you using an existing asset? It is because I want to replicate the same step as you did.

Not sure why you would parent the tool to the player (not the character) once it is equipped

From what I could tell the script is located inside of the Tool.

That is indeed true, but it also looks like it is trying to get the LocalPlayer judging by the variable “player”. It can be a LocalScript. Now, where are still uncertain where is the tool located…

most likely on the player’s backpack. the weird thing is that this guy is defining the tool as the player or parenting the tool TO the player (not certain) when the tool is equipped.

im doing great! and also, the localscript is located inside the tool. and would be inside the character. thats why im parenting the localscript to somewhere that is not in workspace in hopes of it working.

1 Like

ah. i did not notice that. i thought it would be parenting the script to the player. minor mistake on my end

Why would you parent the Localscript somewhere else as soon as the Tool is equipped? This is most likely the reason your code isn’t working, because you define Tool as script.Parent, which is the parent of the script it is looking for. You parented it into the player when it is equipped, and now the script thinks YOU are the tool.

Feel free to correct me if I’m wrong.

The localscript will work fine as long as it is in the tool, it won’t break when it’s actually being used by the character.

i already deleted that part of the code, still doesnt work though

In that case, try removing SetDevicePref() for now and test it. (The line where you fire it in the .Equipped function)

Learn how to use breakpoints, traceback your code and debug.