You can write your topic however you want, but you need to answer these questions: What do you want to achieve? for code to run again after player equipped tool
What solutions have you tried so far? dev hub, looking dev forum posts, adding while loop.
i want for RopeConstrait.Length < 7 code to run every time i equip the tool but it runs only once
server:
wait(game.Loaded)
--// Statements
local LMB_Remote = script.Parent.LMB_Remote
local E_Key_Remote = script.Parent.E_Key_Remote
local Q_Key_Remote = script.Parent.Q_Key_Remote
local E_Key_Connection: RBXScriptConnection = nil
local Q_Key_Connection: RBXScriptConnection = nil
local LMB_Connection: RBXScriptConnection = nil
local tool = script.Parent
--//Main Code
tool.Equipped:Connect(function()
LMB_Connection = LMB_Remote.OnServerEvent:Connect(function(player, target)
local Distance = (player.Character.HumanoidRootPart.Position - target.Position).Magnitude
local Connection_Distance = 50
local Maximum_Length = 50
local minimumLength = 5.5
local decrease = 0.5
local increase = 0.5
local Rope_Attached = target.Rope_Attached.Value
if Rope_Attached == false and (player.Character.HumanoidRootPart.Position - target.Position).Magnitude < Connection_Distance then
target.Rope_Attached.Value = true
local ropeconstraint = Instance.new("RopeConstraint")
local attachment0 = Instance.new("Attachment")
local attachment1 = Instance.new("Attachment")
local blocks = target.Parent
if blocks.Name == player.Name then
print("Distance is "..Distance.." Connection Distance is "..Connection_Distance)
attachment0.Parent = target
attachment1.Parent = script.Parent.Parent.Torso
ropeconstraint.Attachment0 = attachment0
ropeconstraint.Attachment1 = attachment1
ropeconstraint.Color = BrickColor.new("Black")
ropeconstraint.Visible = true
ropeconstraint.Parent = script.Parent.Parent.Torso
ropeconstraint.Length = Distance
if ropeconstraint.Length < minimumLength then
ropeconstraint.Length = minimumLength
decrease = 0
end
if ropeconstraint.Length < 7 then
player.Character.Humanoid.JumpPower = 0
decrease = 0.5
end
if ropeconstraint.Length > minimumLength then
decrease = 0.5
end
if ropeconstraint.Length > 7 then
player.Character.Humanoid.JumpPower = 50
decrease = 0.5
end
if ropeconstraint.Length > Maximum_Length then
increase = 0
end
if ropeconstraint.Length < Maximum_Length then
increase = 0.5
end
end
E_Key_Connection = E_Key_Remote.OnServerEvent:Connect(function()
ropeconstraint.Length += increase
end)
Q_Key_Connection = Q_Key_Remote.OnServerEvent:Connect(function()
player.Character.Torso.Anchored = true
ropeconstraint.Length -= decrease
wait()
player.Character.Torso.Anchored = false
end)
--//Rope length Code
ropeconstraint.Changed:Connect(function()
if ropeconstraint.Length < minimumLength then
ropeconstraint.Length = minimumLength
decrease = 0
end
if ropeconstraint.Length < 7 then
player.Character.Humanoid.JumpPower = 0
decrease = 0.5
end
if ropeconstraint.Length > minimumLength then
decrease = 0.5
end
if ropeconstraint.Length > 6.9 then
player.Character.Humanoid.JumpPower = 50
decrease = 0.5
end
if ropeconstraint.Length > Maximum_Length then
increase = 0
end
if ropeconstraint.Length < Maximum_Length then
increase = 0.5
end
end)
end
--//Tool Unequipped
tool.Unequipped:Connect(function()
E_Key_Connection:Disconnect()
Q_Key_Connection:Disconnect()
LMB_Connection:Disconnect()
end)
end)
end)
wait(game.Loaded)
--// Statements
local LMB_Remote = script.Parent.LMB_Remote
local E_Key_Remote = script.Parent.E_Key_Remote
local Q_Key_Remote = script.Parent.Q_Key_Remote
local E_Key_Connection: RBXScriptConnection = nil
local Q_Key_Connection: RBXScriptConnection = nil
local LMB_Connection: RBXScriptConnection = nil
local E_Key_Connection_InputEnded: RBXScriptConnection = nil
local Q_Key_Connection_InputEnded: RBXScriptConnection = nil
local tool = script.Parent
local player = game.Players.LocalPlayer
local User_Input_Service = game:GetService("UserInputService")
--// Main Code
tool.Equipped:Connect(function(mouse)
mouse.Button1Down:Connect(function()
if mouse.Target.Rope_Attached.Value == false then
LMB_Remote:FireServer(mouse.Target, mouse.TargetSurface)
end
end)
local function E_Key_Pressed(Input_Object, Game_Processed_Event)
if not Game_Processed_Event then
if Input_Object.KeyCode == Enum.KeyCode.E then
E_Key_Remote:FireServer()
end
end
end
local function Q_Key_Pressed(Input_Object, Game_Processed_Event)
if not Game_Processed_Event then
if Input_Object.KeyCode == Enum.KeyCode.Q then
Q_Key_Remote:FireServer()
end
end
end
--// Connecting Functions
E_Key_Connection = User_Input_Service.InputBegan:Connect(E_Key_Pressed)
Q_Key_Connection = User_Input_Service.InputBegan:Connect(Q_Key_Pressed)
local function E_Key_Ended(Input_Object, Game_Processed_Event)
if not Game_Processed_Event then
if Input_Object.KeyCode == Enum.KeyCode.E then
E_Key_Connection:Disconnect()
end
end
end
local function Q_Key_Ended(Input_Object, Game_Processed_Event)
if not Game_Processed_Event then
if Input_Object.KeyCode == Enum.KeyCode.Q then
Q_Key_Connection:Disconnect()
end
end
end
E_Key_Connection_InputEnded = User_Input_Service.InputEnded:Connect(E_Key_Pressed)
Q_Key_Connection_InputEnded = User_Input_Service.InputEnded:Connect(Q_Key_Pressed)
--// Disconnecting Functions
tool.Unequipped:Connect(function()
E_Key_Connection:Disconnect()
Q_Key_Connection:Disconnect()
E_Key_Connection_InputEnded:Disconnect()
Q_Key_Connection_InputEnded:Disconnect()
end)
end)
You have quite a few nested event listeners in your code that are likely the cause of the problem.
I’ve done some de-nesting and a few small cleanups. I haven’t tested my changes but at a glance they look alright. Give this a try:
if not game:IsLoaded() then
game.Loaded:Wait()
end
--// Statements
local LMB_Remote = script.Parent.LMB_Remote
local E_Key_Remote = script.Parent.E_Key_Remote
local Q_Key_Remote = script.Parent.Q_Key_Remote
local E_Key_Connection = nil
local Q_Key_Connection = nil
local LMB_Connection = nil
local tool = script.Parent
local ropeconstraint = Instance.new("RopeConstraint")
local attachment0 = Instance.new("Attachment")
local attachment1 = Instance.new("Attachment")
local Connection_Distance = 50
local Maximum_Length = 50
local minimumLength = 5.5
local decrease = 0.5
local increase = 0.5
--//Rope length Code
local function Handle_Rope_Increment()
if ropeconstraint.Length < minimumLength then
ropeconstraint.Length = minimumLength
decrease = 0
end
if ropeconstraint.Length < 7 then
player.Character.Humanoid.JumpPower = 0
decrease = 0.5
end
if ropeconstraint.Length > minimumLength then
decrease = 0.5
end
if ropeconstraint.Length > 7 then
player.Character.Humanoid.JumpPower = 50
decrease = 0.5
end
if ropeconstraint.Length > Maximum_Length then
increase = 0
end
if ropeconstraint.Length < Maximum_Length then
increase = 0.5
end
end
--//Main Code
tool.Equipped:Connect(function()
E_Key_Connection = E_Key_Remote.OnServerEvent:Connect(function()
ropeconstraint.Length += increase
end)
Q_Key_Connection = Q_Key_Remote.OnServerEvent:Connect(function()
player.Character.Torso.Anchored = true
ropeconstraint.Length -= decrease
wait()
player.Character.Torso.Anchored = false
end)
LMB_Connection = LMB_Remote.OnServerEvent:Connect(function(player, target)
local Distance = (player.Character.HumanoidRootPart.Position - target.Position).Magnitude
local Rope_Attached = target.Rope_Attached.Value
if Rope_Attached == false and (player.Character.HumanoidRootPart.Position - target.Position).Magnitude < Connection_Distance then
target.Rope_Attached.Value = true
local blocks = target.Parent
if blocks.Name == player.Name then
print("Distance is "..Distance.." Connection Distance is "..Connection_Distance)
attachment0.Parent = target
attachment1.Parent = script.Parent.Parent.Torso
ropeconstraint.Attachment0 = attachment0
ropeconstraint.Attachment1 = attachment1
ropeconstraint.Color = BrickColor.new("Black")
ropeconstraint.Visible = true
ropeconstraint.Parent = script.Parent.Parent.Torso
ropeconstraint.Length = Distance
Handle_Rope_Increment()
end
end
end)
end)
--//Tool Unequipped
tool.Unequipped:Connect(function()
E_Key_Connection:Disconnect()
Q_Key_Connection:Disconnect()
LMB_Connection:Disconnect()
end)
ropeconstraint.Changed:Connect(Handle_Rope_Increment)
That’s strange, maybe try removing the first three lines then? I don’t think those are necessary in a server script anyway, just wasn’t sure if you had some need of it.
Hmm, I’m not sure exactly why that would be happening, but I realized there were a lot of references to a player in the code that no longer were valid in my changes, this should fix those. Maybe give this another try?
--// Statements
local LMB_Remote = script.Parent.LMB_Remote
local E_Key_Remote = script.Parent.E_Key_Remote
local Q_Key_Remote = script.Parent.Q_Key_Remote
local E_Key_Connection = nil
local Q_Key_Connection = nil
local LMB_Connection = nil
local Rope_Connection = nil
local tool = script.Parent
local character = nil
local ropeconstraint = Instance.new("RopeConstraint")
local attachment0 = Instance.new("Attachment")
local attachment1 = Instance.new("Attachment")
local Connection_Distance = 50
local Maximum_Length = 50
local minimumLength = 5.5
local decrease = 0.5
local increase = 0.5
--//Rope length Code
local function Handle_Rope_Increment(character)
if ropeconstraint.Length < minimumLength then
ropeconstraint.Length = minimumLength
decrease = 0
end
if ropeconstraint.Length < 7 then
character.Humanoid.JumpPower = 0
decrease = 0.5
end
if ropeconstraint.Length > minimumLength then
decrease = 0.5
end
if ropeconstraint.Length > 7 then
player.Character.Humanoid.JumpPower = 50
decrease = 0.5
end
if ropeconstraint.Length > Maximum_Length then
increase = 0
end
if ropeconstraint.Length < Maximum_Length then
increase = 0.5
end
end
--//Main Code
tool.Equipped:Connect(function()
character = tool.Parent
E_Key_Connection = E_Key_Remote.OnServerEvent:Connect(function(player)
if player.Character == character then
ropeconstraint.Length += increase
end
end)
Q_Key_Connection = Q_Key_Remote.OnServerEvent:Connect(function(player)
if player.Character == character then
character.Torso.Anchored = true
ropeconstraint.Length -= decrease
wait()
character.Torso.Anchored = false
end
end)
LMB_Connection = LMB_Remote.OnServerEvent:Connect(function(player, target)
if player.Character == character then
local Distance = (character.PrimaryPart.Position - target.Position).Magnitude
local Rope_Attached = target.Rope_Attached.Value
if Rope_Attached == false and (character.PrimaryPart.Position - target.Position).Magnitude < Connection_Distance then
target.Rope_Attached.Value = true
local blocks = target.Parent
if blocks.Name == character.Name then
print("Distance is "..Distance.." Connection Distance is "..Connection_Distance)
attachment0.Parent = target
attachment1.Parent = character.PrimaryPart
ropeconstraint.Attachment0 = attachment0
ropeconstraint.Attachment1 = attachment1
ropeconstraint.Color = BrickColor.new("Black")
ropeconstraint.Visible = true
ropeconstraint.Parent = character.PrimaryPart
ropeconstraint.Length = Distance
Handle_Rope_Increment(character)
end
end
end
end)
Rope_Connection = ropeconstraint.Changed:Connect(function()
Handle_Rope_Increment(character)
end)
end)
--//Tool Unequipped
tool.Unequipped:Connect(function()
E_Key_Connection:Disconnect()
Q_Key_Connection:Disconnect()
LMB_Connection:Disconnect()
Rope_Connection:Disconnect()
end)
--// Statements
local LMB_Remote = script.Parent.LMB_Remote
local E_Key_Remote = script.Parent.E_Key_Remote
local Q_Key_Remote = script.Parent.Q_Key_Remote
local E_Key_Connection = nil
local Q_Key_Connection = nil
local LMB_Connection = nil
local Rope_Connection = nil
local tool = script.Parent
local character = nil
local ropeconstraint = Instance.new("RopeConstraint")
local attachment0 = Instance.new("Attachment")
local attachment1 = Instance.new("Attachment")
local Connection_Distance = 50
local Maximum_Length = 50
local minimumLength = 5.5
local decrease = 0.5
local increase = 0.5
--//Rope length Code
local function Handle_Rope_Increment(character)
if ropeconstraint.Length < minimumLength then
ropeconstraint.Length = minimumLength
decrease = 0
end
if ropeconstraint.Length < 7 then
character.Humanoid.JumpPower = 0
decrease = 0.5
end
if ropeconstraint.Length > minimumLength then
decrease = 0.5
end
if ropeconstraint.Length > 7 then
character.Humanoid.JumpPower = 50
decrease = 0.5
end
if ropeconstraint.Length > Maximum_Length then
increase = 0
end
if ropeconstraint.Length < Maximum_Length then
increase = 0.5
end
end
--//Main Code
tool.Equipped:Connect(function()
character = tool.Parent
E_Key_Connection = E_Key_Remote.OnServerEvent:Connect(function(player)
if player.Character == character then
ropeconstraint.Length += increase
end
end)
Q_Key_Connection = Q_Key_Remote.OnServerEvent:Connect(function(player)
if player.Character == character then
character.Torso.Anchored = true
ropeconstraint.Length -= decrease
wait()
character.Torso.Anchored = false
end
end)
LMB_Connection = LMB_Remote.OnServerEvent:Connect(function(player, target)
if player.Character == character then
local Distance = (character.PrimaryPart.Position - target.Position).Magnitude
local Rope_Attached = target.Rope_Attached.Value
if Rope_Attached == false and (character.PrimaryPart.Position - target.Position).Magnitude < Connection_Distance then
target.Rope_Attached.Value = true
local blocks = target.Parent
if blocks.Name == character.Name then
print("Distance is "..Distance.." Connection Distance is "..Connection_Distance)
attachment0.Parent = target
attachment1.Parent = character.PrimaryPart
ropeconstraint.Attachment0 = attachment0
ropeconstraint.Attachment1 = attachment1
ropeconstraint.Color = BrickColor.new("Black")
ropeconstraint.Visible = true
ropeconstraint.Parent = character.PrimaryPart
ropeconstraint.Length = Distance
Handle_Rope_Increment(character)
end
end
end
end)
Rope_Connection = ropeconstraint.Changed:Connect(function()
Handle_Rope_Increment(character)
end)
end)
--//Tool Unequipped
tool.Unequipped:Connect(function()
E_Key_Connection:Disconnect()
Q_Key_Connection:Disconnect()
LMB_Connection:Disconnect()
Rope_Connection:Disconnect()
end)