Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

dash button is making all players move and dont no why?

I made a script for a dash button to work with a Stamina bar. so wen the button is pressed "T" and you have enough stamina it would move your character forwards. but wen the button is pressed it moves every player in the server forwards not just the player who pressed the button. I don't no why Can someone pleas help me.

Stamina client is in starterCharacterScripts and is a local script

local userInputService = game:GetService("UserInputService")
local replicatedStorage = game:GetService("ReplicatedStorage")
local players = game:GetService("Players")

Debounce = true

userInputService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.T and Debounce == true then
		replicatedStorage.RemoteEvents.Dash:FireServer("Began")
		Debounce = false
	elseif input.KeyCode == Enum.KeyCode.ButtonY and Debounce == true then
		replicatedStorage.RemoteEvents.Dash:FireServer("Began")
		Debounce = false
	end
end) 


userInputService.InputEnded:Connect(function(input)
	
		
	if input.KeyCode == Enum.KeyCode.T and Debounce == false then
		replicatedStorage.RemoteEvents.Dash:FireServer("Ended")
		Debounce = true
	elseif input.KeyCode == Enum.KeyCode.ButtonY and Debounce == false then
		replicatedStorage.RemoteEvents.Dash:FireServer("Ended")
		Debounce = true
	end
end)

replicatedStorage.RemoteEvents.DashDistance.OnClientEvent:Connect(function(Dash)
	for index, player in pairs(players:GetChildren()) do
	local Char = player.Character or player.CharacterAdded;wait()
	
	local Humanoid = player.Character.Humanoid
	local Anima = 'rbxassetid://4292176146'
	
	local Animation3 = Instance.new("Animation")
	Animation3.AnimationId = Anima
	local LoadAnimation = Humanoid:LoadAnimation(Animation3)
		if Dash == true then
	LoadAnimation:Play()
	wait(0.1)
	Char.HumanoidRootPart.Velocity = Char.HumanoidRootPart.CFrame.lookVector*300
		end
	end
end)

replicatedStorage.RemoteEvents.StaminaUpdate.OnClientEvent:Connect(function(stamina, maxStamina)
	players.LocalPlayer.PlayerGui.Stamina.Background.Bar.Size = UDim2.new((stamina / maxStamina),0,1,0)
end)

stamina server is in serverScriptService

local replicatedStorage = game:GetService("ReplicatedStorage")
local players = game:GetService("Players")
local runService = game:GetService("RunService")

local maxStamina = 50000
local staminaRegen = maxStamina / 2000
local sprintStaminaCost = 15000

local sprinttingPlayers = {}
local debouce = true
local timer = true

if timer == true then
	wait(3)
	timer = false
end


players.PlayerAdded:Connect(function(player)
	local stamina = player.Stamina
	
	stamina.Changed:Connect(function(property)
		replicatedStorage.RemoteEvents.StaminaUpdate:FireClient(player, stamina.Value, maxStamina)
	end)
end)

replicatedStorage.RemoteEvents.Dash.OnServerEvent:Connect(function(player, state)
		for index, player in pairs(players:GetChildren()) do
			
		local stamina = player.Stamina
		local name = player.Name
		
		local Dash = false
		
	if state == "Began" and debouce == true and stamina.Value >= sprintStaminaCost then
		Dash = true
		replicatedStorage.RemoteEvents.DashDistance:FireClient(player, Dash)
		debouce = false
		stamina.Value = stamina.Value - sprintStaminaCost
	elseif state == "Ended" and debouce == false then
		wait(1)
		Dash = false
		replicatedStorage.RemoteEvents.DashDistance:FireClient(player, Dash)
		debouce = true
		end
	end
end)

2 answers

✓ Accepted answer

You're using a for loop and looping through all the players in the game on line 28 for the stamina server and line 31 for stamina client.

I believe that is the problem.

I believe the issue comes in at line 28 of your server script. In the line

for index, player in pairs(players:GetChildren()) do

You're overwriting the player variable, defined in line 27, every time the loop runs with whichever player it is currently checking in the list of players. Thus, it's running through every player in the game, checking that every player has the stamina to do the dash.

This discussion is preserved as it was. New answers, comments, and votes are closed.