Can't find path to cloned tool for the life of me

I have a server script that is supposed to do a bunch of weapon effects which it does and has done, but when I clone the tool from replicated storage and give it to the player half of the script doesn’t run

muzzleFlashEvent.OnServerEvent:Connect(function(player, muzzlePosition)
	local character = player.Character or player.CharacterAdded:Wait()
	local tool = character:FindFirstChildWhichIsA("Tool") or character:WaitForChild("Backpack"):FindFirstChildWhichIsA("Tool") -- Change this line
	print(tool)
	if tool then
		local muzzlePart = tool:FindFirstChild("Body"):FindFirstChild("Muzzle"):FindFirstChild("Part") -- Change "Muzzle" to the name of the part in your tool
		if muzzlePart then

this line of code has been the bane of my existence as I cannot figure out what the path would be for the tool, I’ve tried what feels like everything but it always comes up as nil unless the tool is already in the players starterpack then it works fine

2 Likes

I might be misunderstanding something in my implementation of the code but the only error I saw is that, if character is the model (inside of workspace), then the backpack is not a child of that. The backpack is a child of the player (inside of players). So I changed the line to be Player:WaitForChild("Backpack")
This is my code implementation that seems to work as you want it too:
Server Code

print("a2")
wait(2)
local Player = game.Players:GetChildren()[1]
print("b2 "..Player.Name)

game.ServerStorage.ahjuk:Clone().Parent = Player.Backpack

ClientCode

print("a1")
wait(5)
print("a2")

local Player = game.Players.LocalPlayer
local character = Player.Character or Player.CharacterAdded:Wait()
print("a")
wait(2)
print("b")
local tool = character:FindFirstChildWhichIsA("Tool") or Player:WaitForChild("Backpack"):FindFirstChildWhichIsA("Tool") -- Change this line

print(tool)

image
image
Hope this helps! :slight_smile:

Now that I look at yours I think my issue just might be that the code needs to be split into a local and a server, but basically the code is entirely server side and what this part is meant to do is just find the tool then turn a light and particle emitter on and off in it. Which works when the tool is already in startercharacter but I have a local script in a gui button giving the player a tool from replicated storage which doesn’t work which is what is confusing me

muzzleFlashEvent.OnServerEvent:Connect(function(player, muzzlePosition)
	local character = player.Character or player.CharacterAdded:Wait()
	local tool = character:FindFirstChildWhichIsA("Tool") or Player:WaitForChild("Backpack"):FindFirstChildWhichIsA("Tool") -- Change this line
	print(tool)
	if tool then
		local muzzlePart = tool:FindFirstChild("Body"):FindFirstChild("Muzzle"):FindFirstChild("Part") -- Change "Muzzle" to the name of the part in your tool
		if muzzlePart then

			local muzzleEffect = Instance.new("ParticleEmitter")
			muzzleEffect.Parent = muzzlePart
			muzzleEffect.EmissionDirection = "Front"
			muzzleEffect.Texture = "rbxassetid://421803006"
			muzzleEffect.Size = NumberSequence.new(1,1.5)
			muzzleEffect.Lifetime = NumberRange.new(0.1)
			muzzleEffect.Speed = NumberRange.new(0)
			muzzleEffect.Rotation = NumberRange.new(0)
			muzzleEffect.RotSpeed = NumberRange.new(0)
			muzzleEffect.Transparency = NumberSequence.new(0,1)
			muzzleEffect.Rate = 500
			muzzleEffect.LightEmission = 0.4
			muzzleEffect.Brightness = 10
			muzzleEffect.Color = ColorSequence.new(flash)

			local spotLight = Instance.new("PointLight")
			spotLight.Brightness = 1
			spotLight.Range = 10
			spotLight.Color = Color3.fromRGB(255, 208, 89)
			spotLight.Parent = muzzlePart


			local duration = 0.1
			spotLight.Enabled = true
			muzzleEffect.Enabled = true
			wait(duration)
			spotLight.Enabled = false
			muzzleEffect.Enabled = false
			wait(0.2)

			-- Fire the bullet only if it's not from the muzzle flash event
			if muzzlePosition ~= muzzlePart.Position then
				createPlayerBullet(player, muzzlePart.Position, muzzlePosition)
			end

			-- Destroy muzzle effect and spotlight
			muzzleEffect:Destroy()
			spotLight:Destroy()
		end
	end
end)

also heres the entire block of code for this segment just to show what im trying to do better

Ah yes, I understand now. If I understand correctly then there actually should not need to be any extra client side code.
What I think you are doing is, the player clicks a button, then a client script gives the client the tool.
This is a problem because of the client server architecture, where the client does not affect the server. You giving the tool to the player on the client means that the server does not see the tool, as it is only on the client.

What you need to do is make the server give the player the gun. This can be done by using a remote event as so:
client side code

local Player = game.Players.LocalPlayer
local GUI = Player.PlayerGui:WaitForChild("ScreenGui")
GUI.TextButton.Activated:Connect(function()
	game.ReplicatedStorage.RemoteEvent:FireServer()
end)

server side code

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(player)
	local Part = Instance.new("Part",game.Workspace)
	Part.CFrame = player.Character:GetPivot()*CFrame.new(0,5,0)
end)

image
This makes the part on the server rather than on the client. If you give the tool on the server, the code should be able to find the cloned tool.

So in this new way of doing it should I give the tool from serverstorage?

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

local player = Players.LocalPlayer
local StartGUI = script.Parent.Parent -- Assuming the button is a child of StartGUI

local function giveRandomWeapon()
	local workingWeapons = ReplicatedStorage.WorkingWeapons:GetChildren()

	if #workingWeapons > 0 then
		local randomIndex = math.random(1, #workingWeapons)
		local randomWeapon = workingWeapons[randomIndex]:Clone()

		randomWeapon.Parent = player.Backpack -- Assuming you want to put the weapon in the player's backpack

		-- You can alternatively put the weapon into the character's Inventory
		-- randomWeapon.Parent = player.Character

		print("You've received a new weapon:", randomWeapon.Name)
	else
		print("No weapons found!")
	end
end

StartGUI.TextButton.MouseButton1Click:Connect(function()
	giveRandomWeapon()
end)

this was my old give tool script that was client side I had it so the player would get a random weapon from the storage when they clicked the button

just figured it out and it seems to be working, thanks a bunch!

1 Like

The code looks very nice! You can move as much as you want to the server

StartGUI.TextButton.MouseButton1Click:Connect(function()
	RemoteEvent:FireServer()
end)

but all you really need to do on the server is:

local randomWeapon = workingWeapons[randomIndex]:Clone()

		randomWeapon.Parent = player.Backpack -- Assuming you want to put the weapon in the player's backpack

Yes, using serverstorage is a good place to keep the tool.

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