Script runs as many times as the number of players on the server

First LocalScript:

local UserInputService = game:GetService("UserInputService")
local rs =  game.ReplicatedStorage
local re = rs:WaitForChild("PetRE"):WaitForChild("PressingE")

UserInputService.InputBegan:Connect(function(input,gameProccesedEvent)
	if input.KeyCode == Enum.KeyCode.E then
		re:FireServer()
		print("fired server")
	end
end)

Second ServerScript:

local rs =  game.ReplicatedStorage
local re = rs:WaitForChild("PetRE"):WaitForChild("PressingE")

local petModule = require(game.ServerScriptService:WaitForChild("PetModule"))

re.OnServerEvent:Connect(function(player)
	
	if player.Stats.Crystal.Value >= 12000 and player.Stats.PetSpace.Value < 50 then
		
		player.Stats.Crystal.Value = player.Stats.Crystal.Value - 12000
		player.Stats.PetSpace.Value +=1
		
		local pet = petModule.chooseRandomPet()
		
		pet.UUID.Value = game.HttpService:GenerateGUID(true)
		
		local petVal = Instance.new("StringValue")
		petVal.Name = pet.Name
		petVal.Parent = player.PetInventory
		petVal.Value = pet.UUID.Value
		
		print(player)--problem
		
		game.ReplicatedStorage.PetRE.HatchEgg:FireClient(player,pet)
		
	end
	
end)

The problem is that when I’m printing out the player, it prints out my name as many times as the number of players on the server. So it fires the client that many times. This is a part of my egg opening script, so the player opens as many eggs as the number of players on the server. I have no idea what the problem is, so help would be appreciated!

Wait, can I see what

local re = rs:WaitForChild("PetRE"):WaitForChild("PressingE")

is?

It is a remote event.
asdddd

1 Like

Does it print “fired server” the amount of players in the server?

Oh yeah forgot to mention that. Fired server only prints out once for some reason.

What exactly is the problem? Pretty confused by what your question is about.

This is a part of my egg opening script, so the player opens as many eggs as the number of players on the server.

Im 90% sure this won’t do anything but its the only thing I can think of

local UserInputService = game:GetService("UserInputService")
local rs =  game.ReplicatedStorage
local re = rs:WaitForChild("PetRE"):WaitForChild("PressingE")

local player = game:GetService("Players").LocalPlayer

UserInputService.InputBegan:Connect(function(input,gameProccesedEvent)
	if input.KeyCode == Enum.KeyCode.E then
		re:FireServer(player)
		print("fired server")
	end
end)

Okay, I get that but that still doesn’t explain the problem whatsoever. What exactly is going wrong and what is the intended behavior? What needs to do what? Please actually describe what’s going on so we can understand and help.

Yeah it didn’t do anything. Thanks for your help tho!

1 Like

That won’t do anything, ROBLOX automatically passes the player sending the request to the connected function, passing it through manually won’t really fix anything.

Wait, where is this localscript?

Okay my bad. What is going wrong has already been said. The intented behaviour is that the player should only open 1 egg even if there are multiple players on the server. I don’t think there is more detail that needs to be said. Correct me if I’m wrong tho.

1 Like

It is inside StarterGui. Should I put it in ReplicatedFirst?

1 Like

I would put it in StarterCharacterScripts

There absolutely does, you haven’t described anything about your system or where the scripts are, what they do, or even any console screenshots of your problem. We can’t help you when we have no idea what the problem is.

The problem is that the event fires once, but is received X amount of times. X is the amount of players on the server.

Does it print the same player?

Nope, the problem still exists.

Yeah it prints out the same player. Looks like i left out a lot of information. Sorry!