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.
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)
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)
To basically sum up everything that all the others said.
UserInputService is a service which is client sided only, which means you can only use it in a local script.
You will need a IsTyping Parameter inside the InputBegan function to make sure the player isn’t typing in the chat.
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)
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.
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?