When Tool Is Cloned Tool Start Not Working

Hi I am Trying To Make A Tool Giver And And When Tool Is Cloned, Tool Starts Not Working. Apparently The AnimationScript Just Worked

Here Is My ToolGiver Submit Button Script

local Players = game:GetService("Players")
local text1 = script.Parent.ToolName
local text2 = script.Parent.PlayerName
local submit = script.Parent.Submit

submit.MouseButton1Click:Connect(function()
	local success,errorMessage = pcall(function()
		game:GetService("ReplicatedStorage").Tools:FindFirstChild(text1.Text):Clone().Parent = game.Players:FindFirstChild(text2.Text):WaitForChild("Backpack")
	end)
	if success then
		print("Creator Has Gived A Tool")
	else
		print("TooIs Not Valid Or Player Is No Valid To Give The Tool.")
	end
end)

And My Sword Script

script.Parent.Activated:Connect(function()
	local handle = script.Parent.Handle
	handle.Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") then
			print("Activated")
			hit.Parent:FindFirstChild("Humanoid"):TakeDamage(20)
			script.Disabled = true
			wait(1)
			script.Disabled = false
		end
	end)
end)

Submit Button Script Is LocalScript SwordScript Is ServerScript

When you clone something on the client, it doesn’t replicate over to the server, which you can read about more here (Client-Server Runtime | Documentation - Roblox Creator Hub). This means that the ServerScript doesn’t actually run for your player, because the Client is making those actions, not the Server. You can use a ServerScript with either a ClickDetector instead or utilize RemoteEvents to speak to the server so that the server can replicate the tool.

I adjusted the way tool and player names are handled, and I also added additional checks to make sure that the tool and the player exist before attempting to give the tool to the player.

local Players = game:GetService("Players")
local text1 = script.Parent.ToolName
local text2 = script.Parent.PlayerName
local submit = script.Parent.Submit

submit.MouseButton1Click:Connect(function()
    local toolName = text1.Text
    local playerName = text2.Text
    local player = Players:FindFirstChild(playerName)

    if player then
        local tool = game:GetService("ReplicatedStorage").Tools:FindFirstChild(toolName)
        if tool then
            local toolClone = tool:Clone()
            toolClone.Parent = player.Backpack
            print("Creator has given a tool to " .. playerName)
        else
            print("Tool is not valid.")
        end
    else
        print("Player is not valid.")
    end
end)

Modular script version (better):
To make the script more modular, we can create a function for the tool giver functionality. Here’s how you can refactor the code:

local Players = game:GetService("Players")
local text1 = script.Parent.ToolName
local text2 = script.Parent.PlayerName
local submit = script.Parent.Submit

local function giveTool(player, toolName)
    local tool = game:GetService("ReplicatedStorage").Tools:FindFirstChild(toolName)
    if tool then
        local toolClone = tool:Clone()
        toolClone.Parent = player.Backpack
        print("Creator has given a tool to " .. player.Name)
    else
        print("Tool is not valid.")
    end
end

submit.MouseButton1Click:Connect(function()
    local playerName = text2.Text
    local player = Players:FindFirstChild(playerName)

    if player then
        local toolName = text1.Text
        giveTool(player, toolName)
    else
        print("Player is not valid.")
    end
end)

Let me know if this is what you needed and if it works for you. :smiley:

The SwordScript Not Worked When Cloning

So, when you click the button on the server, the sword will clone into the player’s backpack, right? In that case, we could use a RemoteEvent.

localscript:

local Players = game:GetService("Players")
local text1 = script.Parent.ToolName
local text2 = script.Parent.PlayerName
local submit = script.Parent.Submit

local giveToolEvent = game:GetService("ReplicatedStorage"):WaitForChild("GiveToolEvent")

local function giveTool(player, toolName)
    local tool = game:GetService("ReplicatedStorage").Tools:FindFirstChild(toolName)
    if tool then
        local toolClone = tool:Clone()
        toolClone.Parent = player.Backpack
        print("Creator has given a tool to " .. player.Name)
    else
        print("Tool is not valid.")
    end
end

submit.MouseButton1Click:Connect(function()
    local playerName = text2.Text
    local player = Players:FindFirstChild(playerName)

    if player then
        local toolName = text1.Text
        giveToolEvent:FireServer(player, toolName)
    else
        print("Player is not valid.")
    end
end)

server-side:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local GiveToolEvent = ReplicatedStorage:WaitForChild("GiveToolEvent")

GiveToolEvent.OnServerEvent:Connect(function(player, toolName)
    local Backpack = player:WaitForChild("Backpack")
    local tool = ReplicatedStorage.Tools:FindFirstChild(toolName)
    
    if tool then
        local toolClone = tool:Clone()
        toolClone.Parent = Backpack
        print("Creator has given a tool to " .. player.Name)
    else
        warn("Tool is not valid.")
    end
end)

@ahmetygci34 Please remember to add the RemoteEvent named GiveToolEvent and also setup script to ensure that the cloned tool is the correct one.

Ok, so I copy the the first script to localscript because fireserver is only from localscript
then i put script to second code which is server side code and i named correctly to event. However Still not work The ServerScript Is In ReplicatedStorage

  1. Server Script: Place this script in the server, so you should put it in a service called ServerScriptService. This script will listen for the RemoteEvent and clone the tool into the player’s inventory.

  2. ReplicatedStorage: Add a RemoteEvent named “GiveToolEvent” in ReplicatedStorage. This will allow communication between the client and the server to give the tool to the player. Additionally, create a folder inside ReplicatedStorage named “Tools” and place the tools you want to give to players inside this folder.

With these steps completed your system will be set up correctly to give tools to players when the RemoteEvent is triggered

Ekran görüntüsü 2024-02-26 194753
Ekran görüntüsü 2024-02-26 194806
The ToolName Is Correct
Ekran görüntüsü 2024-02-26 194904

This Can Be Infinite Yield Because The “GiveToolEvent” Is Not Here On Tools Folder

here’s the localscript updated @ahmetygci34

localscript:

local Players = game:GetService("Players")
local submit = script.Parent.Submit

local giveToolEvent = game:GetService("ReplicatedStorage"):WaitForChild("GiveToolEvent")

local function giveTool(player, toolName)
    local tool = game:GetService("ReplicatedStorage").Tools:FindFirstChild(toolName)
    if tool then
        local toolClone = tool:Clone()
        toolClone.Parent = player.Backpack
        print("Creator has given a tool to " .. player.Name)
    else
        print("Tool is not valid.")
    end
end

submit.MouseButton1Click:Connect(function()
    local playerName = script.Parent.PlayerName.Text
    local player = Players:FindFirstChild(playerName)

    if player then
        local toolName = "TOOLHERE" -- Replace "TOOLHERE" with the actual name of your tool
        giveToolEvent:FireServer(player, toolName)
    else
        print("Player is not valid.")
    end
end)

Here, replace this with the name of the tool

oh i forgot when the tool is cloned the tool starts not working sorry for the mistakes
the sword script stops working

FOUND SOLUTION

change your serverscript’s runcontext to client also known as localscript and it should work

Ekran görüntüsü 2024-02-28 122510

trying this may result a solution!

your script does not your fault its your serverscripts fault

SECOND SOLUTION

parent the tool into serverstorage

and with your same script will run again!

if your script says invaild serverstorage or serverscriptservice only contain as serverscript since they called serverscript or server

to do this, you need remoteevent if you are on localscript. also put remotevent into replicatedstorage to work

serverscript should be on serverscriptservice

Local Script

script.Parent.MouseButton1Click:Connect(function()
	game.ReplicatedStorage.RemoteEvent:FireServer()
end)

ServerScript

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(plr)
	local tool = game.ServerStorage:WaitForChild("YOURTOOL"):clone()
	tool.Parent = plr.Backpack
end)