Tool not working when cloned and given to player

  1. What do you want to achieve?
    Pressing G gives you a tool from ReplicatedStorage called “Chainsaw” and equips it, I only need some type of modification.

  2. What is the issue?
    The tool doesn’t work but it works perfectly when its in starterpack.

  3. What solutions have you tried so far?
    Disabling the scripts and then enabling them when the tool is in the backpack, adding wait() before the tool is in the backpack and in other parts of the script, looking for solutions in the devforum.

--Chainsaw's server script
local tool = script.Parent
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local players = game:GetService("Players")

local button1DownEvent = ReplicatedStorage:FindFirstChild("Button1Down") or Instance.new("RemoteEvent")
button1DownEvent.Name = "Button1Down"
button1DownEvent.Parent = ReplicatedStorage

local button1UpEvent = ReplicatedStorage:FindFirstChild("Button1Up") or Instance.new("RemoteEvent")
button1UpEvent.Name = "Button1Up"
button1UpEvent.Parent = ReplicatedStorage

local idleAnim = Instance.new("Animation")
idleAnim.AnimationId = "http://www.roblox.com/Asset?ID=18719062788"

local activeAnim = Instance.new("Animation")
activeAnim.AnimationId = "http://www.roblox.com/Asset?ID=18719092982"

local idleTrack
local activeTrack

local chainsawSound = Instance.new("Sound", tool.Handle)
chainsawSound.SoundId = "rbxassetid://7432085171"
chainsawSound.Looped = true
chainsawSound.Volume = 5

local equipped = false
local active = false

local handle = tool:WaitForChild("Handle")

local function dealDamage(hit)
	if hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid") and active then
		local humanoid = hit.Parent:FindFirstChild("Humanoid")
		local character = tool.Parent
		local player = game.Players:GetPlayerFromCharacter(character)

		if player and humanoid.Parent ~= character then
			humanoid:TakeDamage(20)
		end
	end
end

handle.Touched:Connect(dealDamage)

local function onEquipped(player)
	equipped = true
	local humanoid = player.Character:WaitForChild("Humanoid")

	idleTrack = humanoid:LoadAnimation(idleAnim)
	idleTrack.Priority = Enum.AnimationPriority.Action
	idleTrack.Looped = true
	idleTrack:Play()
end

local function onUnequipped()
	equipped = false
	if idleTrack then
		idleTrack:Stop()
	end
	if activeTrack then
		activeTrack:Stop()
	end
	chainsawSound:Stop()
end

local function onButton1Down(player)
	if equipped then
		active = true
		if idleTrack then
			idleTrack:Stop()
		end

		local humanoid = player.Character:WaitForChild("Humanoid")
		activeTrack = humanoid:LoadAnimation(activeAnim)
		activeTrack.Priority = Enum.AnimationPriority.Action
		activeTrack.Looped = true
		activeTrack:Play()

		chainsawSound:Play()
	end
end

local function onButton1Up(player)
	if equipped then
		active = false
		if activeTrack then
			activeTrack:Stop()
		end

		local humanoid = player.Character:WaitForChild("Humanoid")
		if not idleTrack then
			idleTrack = humanoid:LoadAnimation(idleAnim)
			idleTrack.Priority = Enum.AnimationPriority.Action
			idleTrack.Looped = true
		end
		idleTrack:Play()

		chainsawSound:Stop()
	end
end

tool.Equipped:Connect(function()
	local player = game.Players:GetPlayerFromCharacter(tool.Parent)
	if player then
		onEquipped(player)
	end
end)

tool.Unequipped:Connect(onUnequipped)

button1DownEvent.OnServerEvent:Connect(onButton1Down)
button1UpEvent.OnServerEvent:Connect(onButton1Up)
--Chainsaw tool's localscript
local tool = script.Parent
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local button1DownEvent = ReplicatedStorage:WaitForChild("Button1Down")
local button1UpEvent = ReplicatedStorage:WaitForChild("Button1Up")

tool.Equipped:Connect(function()
	mouse.Button1Down:Connect(function()
		button1DownEvent:FireServer()
	end)

	mouse.Button1Up:Connect(function()
		button1UpEvent:FireServer()
	end)
end)
--StarterPlayerScript's chainsaw giver localscript
local replicatedStorage = game:GetService("ReplicatedStorage")
local userInputService = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local chainsawName = "Chainsaw"

local button1DownEvent = replicatedStorage:WaitForChild("Button1Down")
local button1UpEvent = replicatedStorage:WaitForChild("Button1Up")

local function equipTool()
	local character = player.Character or player.CharacterAdded:Wait()
	local backpack = player:WaitForChild("Backpack")
	local tool = backpack:FindFirstChild(chainsawName)

	if tool then
		local humanoid = character:FindFirstChildOfClass("Humanoid")
		if humanoid then
			humanoid:EquipTool(tool)
		end
	end
end

local function giveAndEquipTool()
	local backpack = player:WaitForChild("Backpack")
	local chainsaw = backpack:FindFirstChild(chainsawName)
	local character = player.Character or player.CharacterAdded:Wait()

	if not chainsaw then
		local chainsawClone = replicatedStorage:FindFirstChild(chainsawName):Clone()
		chainsawClone.Parent = backpack
	end
	equipTool()
end

userInputService.InputBegan:Connect(function(input, gameProcessedEvent)
	if gameProcessedEvent then return end
	if input.KeyCode == Enum.KeyCode.G then
		local character = player.Character or player.CharacterAdded:Wait()
		local equippedTool = character:FindFirstChild(chainsawName)

		if equippedTool then
			equipTool()
		else
			giveAndEquipTool()
		end
	end
end)
1 Like

Is the script in StarterPlayerScripts a LocalScript? LocalScripts are not given access to ServerScripts, therefore any server scripts cloned from the client will not run. You need to use a regular server script inside of ServerScriptService for the giving tool part, and connect it to the localscript via a RemoteEvent

3 Likes

Thanks, now the tool works. (Yes, it was a localscript, now I used a remoteevent along a serverscript in serverscriptservice to make it work)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.