Converting LocalScript to ServerScript

I have a local script from a previous script and I’m trying to convert it to a tool, when I use a LocalScript with the tool, the rope/thread only appears on the clients side, here’s the script I have:

local Plr = game.Players.LocalPlayer
local P1 
local P2
local MD = 1000
local character = Plr.Character
if not character or not character.Parent then
	character = Plr.CharacterAdded:wait()
end

local Point = character:FindFirstChild("HumanoidRootPart")
if not Point then
	Point = character:WaitForChild("HumanoidRootPart")
end

local PlayerMouse = Plr:GetMouse()
local Rope 
local tool = script.Parent

tool.Activated:Connect(function()
	if PlayerMouse.Target and (PlayerMouse.Hit.p - Point.Position).Magnitude < MD and not PlayerMouse.Target.Parent:FindFirstChild("Humanoid") and not PlayerMouse.Target:IsA("Accessory") then

		Rope = Instance.new("RopeConstraint", workspace)
		Rope.Color = BrickColor.new("White")
		Rope.Visible = true
		Rope.Thickness = 0.1

		P2 = Instance.new("Attachment", PlayerMouse.Target)
		P1 = Instance.new("Attachment", Point)

		P1.WorldPosition = Vector3.new(Point.Position.X,Point.Position.Y,Point.Position.Z)
		P2.WorldPosition = PlayerMouse.Hit.p

		Rope.Attachment0 = P1
		Rope.Attachment1 = P2

		Rope.Length = (P1.WorldPosition - P2.WorldPosition).Magnitude + 1.2

		while Rope.Length > 5 do
			wait()
			Rope.Length = Rope.Length - 2
		end
	end
end)

tool.Deactivated:Connect(function()
	Rope.Length = 0
	if game.Workspace:FindFirstChild("RopeConstraint") then 
		game.Workspace.RopeConstraint:Destroy() 
	end
end)

1 Like

I don’t exactly know what you’re trying to do but, if you want to make the rope appears on the server, you can do:
client:

local function onActivated()
   -- ...
   local target, hit = PlayerMouse.Target, PlayerMouse.Hit
   if target and (hit.Position - Point.Position).Magnitude < MD and not target.Parent:FindFirstChild("
Humanoid") and not target:IsA("Accessory") then -- i think you only want parts? if so you can do a IsA("BasePart") check
      fireRemote(target, hit.Position)
   end
end

server:

local function onServerEvent(player, target, hitPos)
   -- do the thing
end

some additional info:

1 Like

Thanks! I incorporated it into the scripts and it works, here’s the finished scripts btw:

LocalScript:

local Plr = game.Players.LocalPlayer
local P1 
local P2
local MD = 1000
local character = Plr.Character
if not character or not character.Parent then
	character = Plr.CharacterAdded:wait()
end

local Point = character:FindFirstChild("HumanoidRootPart")
if not Point then
	Point = character:WaitForChild("HumanoidRootPart")
end

local PlayerMouse = Plr:GetMouse()
local Rope 
local tool = script.Parent
local remoteA = game.ReplicatedStorage.AbilityEvents.WidowThreadA
local remoteD = game.ReplicatedStorage.AbilityEvents.WidowThreadD

local function onActivated()
	local target, hit = PlayerMouse.Target, PlayerMouse.Hit
	if target and (hit.Position - Point.Position).Magnitude < MD and not target.Parent:FindFirstChild("Humanoid") and not target:IsA("Accessory") then
		remoteA:FireServer(target, hit.Position, Point)
	end
end

local function onDeactivated()
	remoteD:FireServer()
end

tool.Activated:Connect(onActivated)
tool.Deactivated:Connect(onDeactivated)
tool.Unequipped:Connect(onDeactivated)

ServerScript:

local remoteA = game.ReplicatedStorage.AbilityEvents.WidowThreadA
local remoteD = game.ReplicatedStorage.AbilityEvents.WidowThreadD
local P1 
local P2
local MD = 1000

remoteA.OnServerEvent:Connect(function(player, target, hitPos, Point)
	local PlayerMouse = player:GetMouse()

	Rope = Instance.new("RopeConstraint", workspace)
	Rope.Color = BrickColor.new("White")
	Rope.Visible = true
	Rope.Thickness = 0.1

	P2 = Instance.new("Attachment", target)
	P1 = Instance.new("Attachment", Point)

	P1.WorldPosition = Vector3.new(Point.Position.X,Point.Position.Y,Point.Position.Z)
	P2.WorldPosition = hitPos

	Rope.Attachment0 = P1
	Rope.Attachment1 = P2

	Rope.Length = (P1.WorldPosition - P2.WorldPosition).Magnitude + 1.2

	while Rope.Length > 5 do
		wait()
		Rope.Length = Rope.Length - 2
	end
end)

remoteD.OnServerEvent:Connect(function(player)
	Rope.Length = 0
	if game.Workspace:FindFirstChild("RopeConstraint") then 
		game.Workspace.RopeConstraint:Destroy() 
	end
end)
1 Like

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