Why when I press a key the game runs the script for everyone

Hello, I want to make magic attacks but when I press the key with the UIS the game executes the script either for everyone or only for one player while the UIS script is in a local script
I don’t really have a solution, someone could help me pls ?

-- Local script

local UIS = game:GetService("UserInputService")

UIS.InputBegan:Connect(function(key,IsTyping)
	if not IsTyping then
		if key.KeyCode == Enum.KeyCode.F then
			game.ReplicatedStorage.Bri.AirPush.AirPushEvent:FireServer()
		end
	end
end)

UIS.InputEnded:Connect(function(key,IsTyping)
	if not IsTyping then
		if key.KeyCode == Enum.KeyCode.F then
			game.ReplicatedStorage.Bri.AirPush.AirPushEvent.Stop:FireServer()
		end
	end
end)

-- Server

game.ReplicatedStorage.Bri.AirPush.AirPushEvent.OnServerEvent:Connect(function(player)
	local Greuu = player.TransformationFolder.Greuu
	print("True")
	local char = player.Character
	local part = game.ReplicatedStorage.Bri.AirPush.AirEffect:Clone()
	part.Parent = workspace
	part.CFrame = char.HumanoidRootPart.CFrame * CFrame.new(0,0,-68)
	if Greuu.Value == true then
		part.Size = Vector3.new(51, 7.95, 175)
	end
	local charroot = char.HumanoidRootPart
	game.ReplicatedStorage.Bri.AirPush.AirPushEvent.Stop.OnServerEvent:Connect(function()
	part:Destroy()
	end)
	
	part.Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid")  then
			if hit.Parent:FindFirstChild("Humanoid").Parent.Name ~= char.Name then
					wait()
					print("Condition are Done!")
					local root = hit.Parent:FindFirstChild("Humanoid").Parent.HumanoidRootPart
				if Greuu.Value == true then
					hit.Parent:FindFirstChild("Humanoid"):TakeDamage(2.85)
				else
					hit.Parent:FindFirstChild("Humanoid"):TakeDamage(.95)
				end
					local velo = Instance.new("BodyVelocity", a)
					velo.MaxForce = Vector3.new(10000000,10000000,10000000)
					velo.Velocity = (charroot.CFrame.lookVector)*85
					wait(0.3)
					velo:Destroy()
				end
			end
		end)
end)

The simple answer is that you are doing the magic on the server, because you called FireServer. The only thing you are doing on the client is listening for input

You’re using the onserverevent instead of onclient event

Ok you advise me to do what (sorry I am a beginner)

1 Like

Simply replace OnServerEvent with OnClientEvent. You might need to do few more changes if needed.

I just replaced it but it doesn’t work anymore the script don’t run

1 Like

Instead of doing either make the events into functions in the local script and simply call them, for your use case delete the following script:

And instead replace it with the following functions in your local script:

--// Serivces
local Players = game:GetService("Players")

--// Variables
local player = Players.LocalPlayer

--// Functions
function AirPush()
     local Greuu = player.TransformationFolder.Greuu
	print("True")
	local char = player.Character
	local part = game.ReplicatedStorage.Bri.AirPush.AirEffect:Clone()
	part.Parent = workspace
	part.CFrame = char.HumanoidRootPart.CFrame * CFrame.new(0,0,-68)
	if Greuu.Value == true then
		part.Size = Vector3.new(51, 7.95, 175)
	end
	local charroot = char.HumanoidRootPart
	game.ReplicatedStorage.Bri.AirPush.AirPushEvent.Stop.OnServerEvent:Connect(function()
	part:Destroy()
	end)
	
	part.Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid")  then
			if hit.Parent:FindFirstChild("Humanoid").Parent.Name ~= char.Name then
					wait()
					print("Condition are Done!")
					local root = hit.Parent:FindFirstChild("Humanoid").Parent.HumanoidRootPart
				if Greuu.Value == true then
					hit.Parent:FindFirstChild("Humanoid"):TakeDamage(2.85)
				else
					hit.Parent:FindFirstChild("Humanoid"):TakeDamage(.95)
				end
					local velo = Instance.new("BodyVelocity", a)
					velo.MaxForce = Vector3.new(10000000,10000000,10000000)
					velo.Velocity = (charroot.CFrame.lookVector)*85
					wait(0.3)
					velo:Destroy()
				end
			end
		end)

Then replace the following line

with:
AirPush()

I will try but don’t you think that the part will be visible only on the client side?

1 Like

Yeah, isn’t that what you are trying to accomplish? Your topic is Why when I press a key the game runs the script for everyone

OMG I’m sorry, I didn’t explain well (I don’t speak English well) but when I press F, it activates the power for everyone as if everyone pressed F at the same time when one person pressed :sweat_smile: