i want make a Tool with a script inside what can be a Proximity Stun, like if i press "P" it activate and when players get close to me, they are stunned, and it i press P again, it deactivate. i made a script to test the proximity thing, but it dont work, it this:
local players = game.Players local player = script.Parent.Parent.Parent local stuncooldown = false local char = player.Character while true do --loop so it repeatedly occurs if stuncooldown == false then stuncooldown = true spawn(function() --so that it occurs approximately once per second wait(1) stuncooldown = false end) for i, v in pairs(players:GetChildren()) do if (v.Character.Head.Position - char.Head.Position).magnitude <=10 then v.Character.Humanoid.WalkSpeed = 1 v.Character.Humanoid.JumpPower = 0 spawn(function() wait(.5) --below here to unstun player after X seconds v.Character.Humanoid.WalkSpeed = 16 v.Character.Humanoid.JumpPower = 50default end) end end end end
You're going to want to be using a local script within your tool to setup the client side. You should be doing something like this (I also added comments to explain some things you did wrong):
local Players = game:GetService("Players") -- You used game.Players, exploiters can rename the services locally and error your scripts. This is just more reliable. local Player = Players.LocalPlayer -- No need to be attempting to get the player with spamming .Parent, LocalPlayer points to your Player. local Character = Player.Character or Player.CharacterAdded:Wait() -- This is a bit more advanced syntax, you are more or less checking to see if the Character exists, and if it doesn't simply wait for it to exist. local StunCooldown = false local IsStunOn = false game:GetService("UserInputService").InputBegan:Connect(function(Input, GameProcessed) -- This is capturing when a key is pressed if (GameProcessed) then return end -- This is to make sure they aren't trying to say something in chat if (Input.KeyCode == Enum.KeyCode.P) then -- This is checking to make sure the key they pressed was P if (IsStunOn) then -- This is checking to see if the stun effect is on IsStunOn = false -- If it's on, turn it off else -- If its not on: if (StunCooldown == false) then -- Make sure they aren't in the middle of a cooldown StunCooldown = true -- Turn on their cooldown IsStunOn = true -- Turn on the IsStunOn bool check game:GetService("ReplicatedStorage").RemoteEvent:FireServer() -- Fire a remove event without any arguments for security reasons wait(1) -- Wait a second to get rid of the effect and cooldown IsStunOn = false -- Turn off the effect StunCooldown = false -- Turn off the cooldown end end end end)
On the server, you should be doing something like this:
game:GetService("ReplicatedStorage").RemoteEvent.OnServerEvent:Connect(function(Player) -- Listen for the event to be called, the Player who fired the remote is automatically the first argument local Character = Player.Character or Player.CharacterAdded:Wait() -- Get the players character or wait for it to exist for _, OtherPlayer in pairs(game:GetService("Players"):GetPlayers()) do -- Get every player in the server coroutine.resume(coroutine.create(function() -- Create a coroutine / simulate a new OS thread, Lua doesn't actually use more than one OS thread though. if (OtherPlayer == Player) then return end -- Make sure it's not the player who fired the remote local OtherPlayerCharacter = OtherPlayer.Character -- Get their character local OtherPlayerHumanoidRootPart = OtherPlayerCharacter.HumanoidRootPart -- Get their humanoid root part if (OtherPlayerHumanoidRootPart.Position - Character.HumanoidRootPart.Position).Magnitude < 10 then -- Make sure they're within 10 studs OtherPlayerCharacter.Humanoid.WalkSpeed = 1 -- Set their WS to 1 OtherPlayerCharacter.Humanoid.JumpPower = 0 -- Set their JP to 0 wait(0.5) -- Yeild for half a second OtherPlayerCharacter.Humanoid.WalkSpeed = 16 -- Set their WS to normal OtherPlayerCharacter.Humanoid.JumpPower = 50 -- Set their JP to normal end end)) end end)
Keep in mind, this is inherently unsecure because every player has network ownership of their Character, meaning all physics is handled by them and replicated to the server. They can change their WalkSpeed on their own and this system can overall be exploited. You should also make checks separately to determine if they are exploiting their WalkSpeed.
Hope this helps.