Help with making code work

:wave:

  1. What do you want to achieve?
    I am trying to make a GUI which when you click freezes the player by welding it to a part when you click it and when you click it again it removes all the weld from the player so the player can move.

  2. What is the issue?
    The code keeps breaking. It does welds the part but I am having problem with making it to toggle. I cant understand/ dont know how to achieve this.

  3. What solutions have you tried so far?

I have tried making many toggle variables which will check if the code has been toggled or not but it keeps breaking. I have spent 5+ hours on this script trying to figure out and debugging it. I am very confused now.

Code:

Client-Side:

local eventOn = game.ReplicatedStorage.Events.WeldThisNoob
local toggle = false
local eventOff = game.ReplicatedStorage.Events.UnWeldThisNoob

script.Parent.MouseButton1Click:Connect(function()
	if toggle == false then
		toggle = true
		eventOn:FireServer() 
	else
		toggle = false
		eventOff:FireServer()
	end
end)

Server Side:

local repEvent = game.ReplicatedStorage.Events.WeldThisNoob
local part = game.Workspace.Model.Part
local eventOff = game.ReplicatedStorage.Events.UnWeldThisNoob
local activate = false
local deactivate = false

repEvent.OnServerEvent:Connect(function()
	    local activate = true
        local deactivate = false 
	    part.Touched:Connect(function(hit)
	    if activate == true and deactivate == false then
		hit.Parent.Humanoid.WalkSpeed = 0
		hit.Parent.Humanoid.JumpPower = false
		
		hit.Parent:WaitForChild("Right Arm")
		hit.Parent:WaitForChild("Left Arm")
		hit.Parent:WaitForChild("Right Leg")
		hit.Parent:WaitForChild("Left Leg")
		hit.Parent:WaitForChild("Torso")
		hit.Parent:WaitForChild("Head")
		
		local LeftFoot = Instance.new("WeldConstraint")
		LeftFoot.Parent = hit.Parent
		LeftFoot.Part0 = part
		LeftFoot.Part1 = hit.Parent["Left Leg"]
		
		local RightFoot = Instance.new("WeldConstraint")
		RightFoot.Parent = hit.Parent
		RightFoot.Part0 = part
		RightFoot.Part1 = hit.Parent["Right Leg"]
		
		local RightArm = Instance.new("WeldConstraint")
		RightArm.Parent = hit.Parent
		RightArm.Part0 = part
		RightArm.Part1 = hit.Parent["Right Arm"]
		
		local LeftArm = Instance.new("WeldConstraint")
		LeftArm.Parent = hit.Parent
		LeftArm.Part0 = part
		LeftArm.Part1 = hit.Parent["Left Arm"]
		
		local Head = Instance.new("WeldConstraint")
		Head.Parent = hit.Parent
		Head.Part0 = part
		Head.Part1 = hit.Parent["Head"]
	    end
	
		while wait() do
		
		if activate == false and deactivate == true then
	    
		if hit.Parent.Humanoid then
		hit.Parent.Humanoid.WalkSpeed = 16
		hit.Parent.Humanoid.JumpPower = 50
        local Character = hit.Parent
        for i, y in ipairs(Character:GetDescendants()) do
        if y:IsA("WeldConstraint") then
        y:Destroy()
        end
        end
		end
		end
		end 
	end)
end)

 
eventOff.OnServerEvent:Connect(function()
activate = false
deactivate = true
end)

while wait(5) do
	print( deactivate, activate)
end

Any ideas or tip on how I can achieve this will be very much appreciated

1 Like

For the client script, you could make the toggling logic like this:

local eventOn = game.ReplicatedStorage.Events.WeldThisNoob
local eventOff = game.ReplicatedStorage.Events.UnWeldThisNoob
local toggle = false

script.Parent.MouseButton1Click:Connect(function()
    toggle = not toggle
    if toggle then
        eventOn:FireServer() 
    else
        eventOff:FireServer()
    end
end)

For the server script, are you planning to freeze mulitple players? Or just one player?

2 Likes

I am trying to freeze all the players touching a specific part. So, yes there are many players.

1 Like

The variables seem to be defined locally, maybe define them globally?

activate = true
deactivate = false
2 Likes

Thank you @wevetments and @ReturnedTrue for you help.
I did some changes and the script works flawlessly.
sigh- I feel so relaxed now thank you so much

2 Likes

If you need a reference, you can take a look at this place that I made
Freeze Character in Area.rbxl (22.4 KB)
Anyone standing on the part will freeze. I made this place in a few minutes, so the code may be messy, but it works as expected. I also added a delay between freezing and unfreezing to avoid lag.

1 Like