ParticleEmitter:Emit() not working

Hi i want to make a particle that appears when the player hits E.
It works if i don’t use UserInputService but if i use it, nothing happens.

This is the script with UIS:

local Particles = game.ReplicatedStorage.Particles.ParticlesAttach:Clone()

local UIS = game:GetService("UserInputService")

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(Character)
		Particles.Parent = Character.HumanoidRootPart
		Particles.ParticleEmitter.Enabled = false
		
		UIS.InputBegan:Connect(function(input)
			if input.KeyCode == Enum.KeyCode.E then
				wait()
				print("emitted")
				Particles.ParticleEmitter:Emit(10)
			end
		end)
	end)
end)

And this one is without UIS:

local Particles = game.ReplicatedStorage.Particles.ParticlesAttach:Clone()

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(Character)
		Particles.Parent = Character.HumanoidRootPart
		Particles.ParticleEmitter.Enabled = false
		wait(5)
		Particles.ParticleEmitter:Emit(10)
	end)
end)

This one works but i want to make that with UIS.

Thank you

2 Likes

You need to use the UIS in localScript and parse some imformation with RemoteEvent.

1 Like

What the person above said. UserInputService is Client sided only.

Also use the second argument of UIS to determine if the player is typing so the emitter is not triggered when typing a word with the letter E.

UIS.InputBegan:Connect(function(input, gpe)
if gpe then return end
-- the rest of the code..
1 Like

UserInputService can only be accessed by the Client. You would need to create a RemoteEvent and fire it whenever certain KeyCode is pressed. You can check on the Server-side if the Player’s Character RootPart has the ParticleEmitter and then Emit it.

UserInputService can only be used to record input on the client’s side.

For some reason it stills doesn’t work.

This is my local script:

local Particles = game.ReplicatedStorage.Particles.ParticlesAttach:Clone()

local UIS = game:GetService("UserInputService")

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(Character)
		Particles.Parent = Character.HumanoidRootPart
		
		UIS.InputBegan:Connect(function(input, gpe)
			if gpe then return end
			
			if input.KeyCode == Enum.KeyCode.E then
				game.ReplicatedStorage.RemoteEvent:FireServer(Character, Particles)
			end
		end)
	end)
end)

and this one my script:

local Re = game.ReplicatedStorage.RemoteEvent

Re.OnServerEvent:Connect(function(player, Character, Particles)
	if Character.HumanoidRootPart:WaitForChildd("ParticlesAttach") then
		wait()
		Particles.ParticleEmitter:Emit(2)
	end
end)

someone knows which is the problem?

localScript

		UIS.InputBegan:Connect(function(input, isTyping)
			if isTyping then return end
			
			if input.KeyCode == Enum.KeyCode.E then
				game.ReplicatedStorage.RemoteEvent:FireServer(Particles)
			end
		end)
	end)
end)

I don’t understand it’s the same only that you changed parameters in fireserver and gpe.

i tried and it didn’t work.

To basically sum up everything that all the others said.

  1. UserInputService is a service which is client sided only, which means you can only use it in a local script.

  2. You will need a IsTyping Parameter inside the InputBegan function to make sure the player isn’t typing in the chat.

  3. You will need to use a RemoteEvent to fire to the server and emit the particles from there.

so it would go something like this

local

UserInputService.InputBegan:Connect(function(input, IsTyping)
if IsTyping then return end --this is used to identify if the player is typing in the chat or not

if input.KeyCode == Enum.KeyCode.E then

RemoteEvent:FireServer()

end
end)

server

RemoteEvent.OnServerEvent:Connect(function(player)

ParticleEmitter:Emit(5)

end)

I am pretty sure that is everything summed up.

I got an error in my script which says: ServerScriptService.Script:11: attempt to index nil with ‘Parent’

My local script is this:

local Particles = game.ReplicatedStorage.Particles

local UIS = game:GetService("UserInputService")

UIS.InputBegan:Connect(function(input, isTyping)
	
	if isTyping then return end
	
	if input.KeyCode == Enum.KeyCode.E then
		
		print("Click")
		
		local ParticlesClone = Particles:Clone()
		
		game.ReplicatedStorage.RemoteEvent:FireServer(ParticlesClone)
		
	end
end)

My script:

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(player, ParticlesClone)
	
	local Character = player.Character
		
		print("¡Received!")
			
		ParticlesClone.Parent = Character.HumanoidRootPart -- Here is the error
			
		ParticlesClone.ParticlesAttach.ParticleEmitter:Emit(3)
			
		wait(5)
		
		ParticlesClone:Destroy()		
end)

Since the particle clone is being cloned on the client, the server won’t see it hence it won’t be able to parent it.

In that case, just clone it on the server script instead and parent it there rather than doing it from the localscript.

Local script: Handles input (Detect E then fire event)
Server Script: recieves event and emits particles.

There isn’t any need to destroy it either, you can actually probably just keep it on the player and hide it, then when you want to emit it, just look into the rootpart of the charcater which is where its parented then :Emit() it from there.

Saves the hassle of creating it and destroying it every time.

1 Like

Thank you so much!! it worked. but it stills have a little problem which is that if i put a wait(no matter what number) it disappears after 3 seconds and i don’t know why

maybe it’s a thing about the emit event but how i can change the amount of time that the emit event lasts?

Oh nvm i already fixed it. i just needed to anchor the particles part hehe sorry