What should I do to make this script work in SeverScript?

I have a question. How would I convert this script to a Serverscript instead of a Local Script?

    script.Parent.Touched:Connect(function(Char)
	local plr = game.Players:GetPlayerFromCharacter(Char.Parent)
	local backpack = plr.Backpack
	local sword = game.Lighting.Storage.ClassicSword:Clone()
	local con = game.Lighting.Storage.HasPvP:Clone()
	if not backpack:FindFirstChild("HasPvP") then
		sword.Parent = backpack
		con.Parent = backpack
		else
		end
end)

script.Parent.TouchEnded:Connect(function(Char)
	local plr = game.Players:GetPlayerFromCharacter(Char.Parent)
	local backpack = plr.Backpack
	if backpack:FindFirstChild("HasPvP") then
		backpack:FindFirstChild("HasPvP"):Destroy()
		backpack:FindFirstChild("ClassicSword"):Destroy()
		Char:FindFirstChild("ClassicSword"):Destroy()
	else
	end
end)

Your script is fine as is. Just copy and paste it into a server script. Although, it’s vulnerable to errors because you don’t check if the player was found:

local players = game:GetService("Players")

script.Parent.Touched:Connect(function(Char)
	local plr = players:GetPlayerFromCharacter(Char.Parent)

    if not plr then
       return
    end

	local backpack = plr.Backpack
	local sword = game.Lighting.Storage.ClassicSword:Clone()
	local con = game.Lighting.Storage.HasPvP:Clone()

	if not backpack:FindFirstChild("HasPvP") then
		sword.Parent = backpack
		con.Parent = backpack
		end
end)

script.Parent.TouchEnded:Connect(function(Char)
	local plr = players:GetPlayerFromCharacter(Char.Parent)

    if not plr then
       return
    end

	local backpack = plr.Backpack
	if backpack:FindFirstChild("HasPvP") then
		backpack:FindFirstChild("HasPvP"):Destroy()
		backpack:FindFirstChild("ClassicSword"):Destroy()
		Char:FindFirstChild("ClassicSword"):Destroy()
	end
end)
1 Like