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.
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)
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
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.
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
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)