I am making something where if you press Q, anyone within 200 studs will stop moving completely, but this won't work in-game, only in Studio.
Here's the code I used:
01 | game.Players.PlayerAdded:Connect( function (player) |
02 | wait(. 1 ) |
03 | hi = player.Name |
04 |
05 | print (hi) |
06 | local player = game.Players [ hi ] |
07 | player:GetMouse().KeyDown:Connect( function (k) |
08 | if k = = "q" and player.Name = = "bearpro2" then |
09 | for i, v in pairs (workspace:GetChildren()) do |
10 | if v:FindFirstChild( "Humanoid" ) then |
11 | if v.Name = = player.Name then |
12 | else |
13 | if (v.Torso.Position - player.Character.Torso.Position).Magnitude < = 200 then |
14 | v.Torso.Anchored = true |
15 | v [ "Right Leg" ] .Anchored = true |
So a few things, you shouldn't be using KeyDown as it is deprecated, so I would recommend using the UserInputService. I also don't think you can get a player's keyboard inputs from the server.
A better way to set this up would be to put this code in a local script, and use a remote event
Create a RemoteEvent in Replicated Storage, name it "FreezeEvent" for now, then create a Local Script in StarterPack with the following code
01 | local player = game:GetService( "Players" ).LocalPlayer |
02 | local char = player.CharacterAdded:wait() -- Waits for the character |
03 |
04 | local UIS = game:GetService( "UserInputService" ) |
05 | local RS = game:GetService( "ReplicatedStorage" ) |
06 | local freeze = RS:WaitForChild( "FreezeEvent" ) -- You can name this remote event whatever you want |
07 |
08 | UIS.InputBegan:connect( function (input,gpe) -- InputBegan event takes 2 parameters, the InputObject and GPE(Game Processed Event) |
09 | if not gpe then -- Makes sure you can't use this ability while typing. |
10 | if input.KeyCode = = Enum.KeyCode.Q and player.Name = = "bearpro2" then -- You can change the controls to whatever |
11 | freeze:FireServer() -- Calls the server via the RemoteEvent |
12 | end |
13 | end |
14 | end ) |
Then create a Script in ServerScriptService with the following code
01 | local UIS = game:GetService( "UserInputService" ) |
02 | local RS = game:GetService( "ReplicatedStorage" ) |
03 | local freeze = RS:WaitForChild( "FreezeEvent" ) -- You can name this remote event whatever you want. |
04 |
05 | -- Listens for the RemoteEvent to be called |
06 | freeze.OnServerEvent:connect( function (player) -- Remote events always return the player who sent the request. |
07 | for i,v in pairs (workspace:GetChildren()) do |
08 | if v:FindFirstChild( "Humanoid" ) and v ~ = player.Character then |
09 | if (v.HumanoidRootPart.Position - player.Character.HumanoidRootPart.Position).magnitude < = 200 then -- use HumanoidRootPart so it's compatible with both R6 and R15 |
10 | for i,child in pairs (v:GetChildren()) do |
11 | -- BasePart includes both Parts and MeshParts |
12 | if child:IsA( "BasePart" ) then -- Loops through all the parts inside the model instead of looking for specific parts. |
13 | child.Anchored = true -- Anchors it. |
14 | end |
15 | end |
16 | end |
17 | end |
18 | end |
19 | end ) |
lol remember to mark me as the answer if this helps you btw