LocalScript not detecting RemoteEvent

Hi,

Recently, I’ve started on my first actual game project, and I’ve ran into a few issues. Most of them I’ve been able to solve myself after a few minutes of googling or searching the DevForum, but this one I haven’t been able to figure out just yet, having spent over 48 hours in total trying to find out the solution:

I have a part set up so that when a player touches it, it blacks the screen out, plays a noise, teleports the player, and then fades the screen back in. So far, I have all of this working except the sound, which I want to play locally. I figured out I need to use a RemoteEvent to have it play for the local user only, so I started looking into how those work and how to use them. As far as I can tell, I seem to be doing everything right, but for whatever reason, the sound isn’t playing. I even set up ‘print’ statements in the LocalScript the event is meant to be triggering, to make sure it’s actually detecting the event, and it doesn’t seem to be.

As you can see, I have the event set up in ReplicatedStorage, along with a copy of the sound I want to play:
replicated

Here’s a few snippets from the script inside the trigger part, and the localscript, which is stored in StarterPlayerScripts:

Trigger part snippets
local players = game:GetService('Players')



local PlayLocalSoundRemote = Instance.new('RemoteEvent', game.ReplicatedStorage)
PlayLocalSoundRemote.Name = 'PlayLocalSoundRemote'

function PlaySoundForPlayer(plr, SoundId)
	print("Running function.")
	for _,i in pairs(game.Players:GetPlayers()) do
		if i == plr then
			print("Player matches. Firing event.")
			PlayLocalSoundRemote:FireClient(plr,SoundId)
			print('Player is ' .. tostring(plr))
			print('SoundId is ' .. tostring(SoundId))
		end
	end
end

Later on in the script, in the touchPart.Touched function:

PlaySoundForPlayer(Player, 'rbxassetid://314390675')
LocalScript
local ReplicatedStorage = game:GetService('ReplicatedStorage')
local playLocalSoundRemote = ReplicatedStorage:WaitForChild('PlayLocalSoundRemote')


local function playSound(SoundId, Volume) 	
	print("got here")
	if Volume == nil then Volume = 1 end
	local Sound = Instance.new('Sound', workspace.CurrentCamera)
	Sound.SoundId = SoundId
	if not Sound.IsLoaded then
		Sound.Loaded:Wait()
	end
	Sound.Volume = Volume
	Sound:Play()
	print("Sound played.")
	game:GetService('Debris'):AddItem(Sound, Sound.TimeLength)
end


playLocalSoundRemote.OnClientEvent:Connect(playSound)

When testing, it seems to fire every single debug print in the trigger part’s script, but none of the print statements in the LocalScript, leading me to believe the localscript isn’t even detecting the event. Any help with this would be greatly appreciated.

1 Like

I’m not a very experienced scripter and all these scripts confuses me, but why are you putting the sound in… workspace.CurrentCamera? If I’m correct, you can put them in the player object in game.Players and other players won’t hear it.

1 Like

I’ll do that once I get the script to actually detect the event in the first place, but thanks.

1 Like

Worth mentioning that you could do this without the remote entirely, unless there’s something I’m unaware of in which you need the server involved. The client can check a part touch and then decide to play the sound. Only extra step needed would be to validate that touches on the part came from the LocalPlayer’s character.

2 Likes

Maybe because it’s a localscript? I’m not a good scripter.

I get confused there myself, actually you can detect the player from their character and put the sound in their player object, then plays it. That should work unless there’s a more efficient way.

Yeah, I was thinking I should just move everything over to a LocalScript instead and save myself the hassle, but it’s still worth figuring out why this isn’t working for when I need to use RemoteEvents in the future, right?

Well, no, that wouldn’t fix it at the moment, because as I’ve said numerous times, the LocalScript isn’t even being triggered in the first place, as none of the print statements inside it are firing.

You don’t need the LocalScript, you can do that in the touched function even though it’s the normal type. Have you heard of game.Players:GetPlayerFromCharacter() atleast once? Pretty much all you need to do is type the character object (e.g. hit.Parent, part.Parent, etc) inside the brackets. I’m not sure if this will work correctly nor properly so it’s up to you if you’ll use this or not.

…Yes, I’m well aware of that. You still aren’t understanding the issue I’m having. I’m detecting the player just fine, like I said, everything in the script inside the trigger part works fine. It’s just when it fires the RemoteEvent, the LocalScript doesn’t detect it. And, no, I do need the LocalScript, because that’s the only way to play a sound for the local user only, as far as I’m aware.

1 Like

Is the LocalScript in StarterPlayerScripts? If it is then I pretty much ran out of solutions.

Yes. I said that in my original post.

1 Like

Is there any errors in the output? You provided plr and SoundId when you FireClient() but the function which handles the FireClient() only handles SoundId and Volume. I don’t know if that’s the case since it’s supposed to prints an error saying SoundId is invalid.

When it comes to RemoteEvents I usually just connect the function first (e.g. OnClientEvent:Connect(function(argument1,argument2, etc)) so I don’t know about writing separate function and connecting it later.
Sorry if I provide no help at all.

There are no errors in the output, no. And the SoundId and Volume arguments shouldn’t matter, since even if they were incorrect, it’d at least fire, right? There is output from the print statements in the serverside script, and that’s it. No errors, nothing.

Are all of these happening in one LocalScript?

All of what? I included snippets of my scripts in the OP, including the entire LocalScript. Please read them.

Oh sorry my native language isn’t English so I have a hard time understanding them. That’s probably all I can do. Try giving the solution I talked about a shot or wait for other possible solutions. Good luck!

I’m going to try putting everything inside a LocalScript later tonight, but I’d still appreciate advice on how to get this working, as I’m probably going to need to use RemoteEvents at some point in the future, anyway.

I’m gonna go with this for now, but do you have anywhere I can look for more info on having the player check if a specific part has been touched in a LocalScript? I can’t seem to find anything on it, but I might just be looking in the wrong places.

This tutorial I wrote a while back might be helpful:

3 Likes