Hey I’m trying to make it so that I can add a sword into a players toolbox using script, i have the event trigger and everything I just don’t know the script that adds the sword into the players toolbox
Hey I’m trying to make it so that I can add a sword into a players toolbox using script, i have the event trigger and everything I just don’t know the script that adds the sword into the players toolbox
Clone the tool into the players backpack. Here’s an example:
local Tool = game.ServerStorage.Tool --Tool location here
local ToolClone = Tool:Clone()
ToolClone.Parent = player.Backpack
The sword is not being given to the players, any idea why?
local SwordGiver = game.ServerStorage.Sword --Tool location here
local SwordClone = SwordGiver:Clone()
SwordClone.Parent = workspace[standingOnPart1.Name].Backpack and workspace[standingOnPart2.Name].Backpack
How are you wanting to give the player the sword? When they join, when they touch a part, etc.
Basically, when a player stands on part 1 and another stands on part 2, a play button appears, and then they are teleported to a part called ‘team1’ and ‘team2’ and i want them to both be given a sword…
Here is my script:
local Players = game:GetService("Players")
local player = game.Players.LocalPlayer
local part1 = workspace.Part1
local part2 = workspace.Part2
local team1 = workspace.team1
local team2 = workspace.team2
local standingOnPart1 = nil
local standingOnPart2 = nil
local RemoteEvent = game.ReplicatedStorage:FindFirstChild("RemoteEvent")
local function startEvent()
local SwordGiver = game.ServerStorage.Sword --Tool location here
local SwordClone = SwordGiver:Clone()
SwordClone.Parent = workspace[standingOnPart1.Name].Backpack and workspace[standingOnPart2.Name].Backpack
local points1 = Instance.new("NumberValue")
local points2 = Instance.new("NumberValue")
workspace[standingOnPart1.Name].points1.Value = 0
workspace[standingOnPart2.Name].points2.Value = 0
workspace[standingOnPart1.Name].Died:Connect(function(died)
workspace[standingOnPart2.Name].points2.Value = points2 + 1
end)
workspace[standingOnPart2.Name].Died:Connect(function(died1)
workspace[standingOnPart1.Name].points1.Value = points1 + 1
end)
if points1 == 1 then
workspace.Baseplate.BrickColor = BrickColor.new("Teal")
end
end
RemoteEvent.OnServerEvent:Connect(function() -- if any player presses the play button, executes event
if standingOnPart2 ~= nil and standingOnPart1 ~= nil then
--Hide playbutton gui
standingOnPart2.PlayerGui.ScreenGui.Enabled = false
standingOnPart1.PlayerGui.ScreenGui.Enabled = false
--Teleports
workspace[standingOnPart1.Name].HumanoidRootPart.CFrame = CFrame.new(team1.Position.X,team1.Position.Y,team1.Position.Z)
workspace[standingOnPart1.Name].HumanoidRootPart.CFrame *= CFrame.Angles(0, math.rad(90), 0)
workspace[standingOnPart2.Name].HumanoidRootPart.CFrame = CFrame.new(team2.Position.X,team2.Position.Y,team2.Position.Z)
workspace[standingOnPart2.Name].HumanoidRootPart.CFrame *= CFrame.Angles(0, math.rad(270), 0)
local gameRunning = true
startEvent() -- triggers function
end
end)
-----------------------------------------------------
-- PART1 Events --
-----------------------------------------------------
-- Touched functions [Events now set each part that was touched to the players name instead of the opposite button]
part1.Touched:Connect(function(TouchedPart) -- Touched
local Player = Players:GetPlayerFromCharacter(TouchedPart.Parent)
if Player then -- Check if it found character
if standingOnPart1 == nil then -- Check if not standing and if not occupied
part1.BrickColor = BrickColor.new("Teal") -- Set color
standingOnPart1 = Player
end
end
end)
part1.TouchEnded:Connect(function(TouchedPart)
local Player = Players:GetPlayerFromCharacter(TouchedPart.Parent)
if Player then -- Check if it found character
if standingOnPart1 ~= nil then -- Check if standing and is occupied
if standingOnPart1.PlayerGui.ScreenGui.Enabled == true then
standingOnPart1.PlayerGui.ScreenGui.Enabled = false
end
part1.BrickColor = BrickColor.new("Smoky grey") -- Set color
standingOnPart1 = nil
if standingOnPart2 ~= nil then
if standingOnPart2.PlayerGui.ScreenGui.Enabled == true then
standingOnPart2.PlayerGui.ScreenGui.Enabled = false
end
end
end
end
end)
-----------------------------------------------------
-- PART2 Events --
-----------------------------------------------------
part2.Touched:Connect(function(TouchedPart) -- Touched
local Player = Players:GetPlayerFromCharacter(TouchedPart.Parent)
if Player then -- Check if it found character
if standingOnPart2 == nil then -- Check if not standing and if not occupied
part2.BrickColor = BrickColor.new("Teal") -- Set color
standingOnPart2 = Player
end
end
end)
part2.TouchEnded:Connect(function(TouchedPart)
local Player = Players:GetPlayerFromCharacter(TouchedPart.Parent)
if Player then -- Check if it found character
if standingOnPart2 ~= nil then -- Check if standing and is occupied
if standingOnPart2.PlayerGui.ScreenGui.Enabled == true then
standingOnPart2.PlayerGui.ScreenGui.Enabled = false
end
part2.BrickColor = BrickColor.new("Smoky grey") -- Set color
standingOnPart2 = nil
if standingOnPart1 ~= nil then
if standingOnPart1.PlayerGui.ScreenGui.Enabled == true then
standingOnPart1.PlayerGui.ScreenGui.Enabled = false
end
end
end
end
end)
while task.wait(0) do
if standingOnPart2 ~= nil and standingOnPart1 ~= nil then -- checks if both players are on top
standingOnPart2.PlayerGui.ScreenGui.Enabled = true
standingOnPart1.PlayerGui.ScreenGui.Enabled = true
end
end
Not sure if this is the issue, but I heard that detecting standing whilst using Touched is extremely unreliable. I’d recommend trying out ZonePlus to detect when players are standing or not
Ok, so the variable standingOnPart1
and standingOnPart2
will have been set to Player
instances. I’m assuming you want the sword to be equipped when they get teleported so first you’ll clone the sword to both players backpack then use :EquipTool on the characters humanoid to actually equip it. You can make this a lot shorter by creating a table variable with the 2 players inside instead of having 2 separate variables so you can loop through the table to give the swords out, but this should work for now.
local SwordGiver = game.ServerStorage.Sword
local Sword1 = SwordGiver:Clone()
Sword1.Parent = standingOnPart1.Backpack
local char = standingOnPart1.Character
if char then
local humanoid = char:FindFirstChild("Humanoid")
if humanoid then
humanoid:EquipTool(Sword1)
end
end
local Sword2 = SwordGiver:Clone()
Sword2.Parent = standingOnPart2.Backpack
local char = standingOnPart2.Character
if char then
local humanoid = char:FindFirstChild("Humanoid")
if humanoid then
humanoid:EquipTool(Sword2)
end
end