Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to make a player make a sound by pressing key?

I know I already asked a question similar to this, and the answer was for me to create a remote event. I did, but I have run into another problem.

What I'm trying to do is make a button that, when you click it, gives you the ability to make a noise by pressing "1" on the keyboard.

I have a Local Script inside the GUI with the button that gives this power to the player.

function onClick()
	local action = game:GetService("ReplicatedStorage"):WaitForChild("MakeSound")
	action:FireServer()
end
script.Parent.MouseButton1Click:Connect(onClick)

Then, I have a remote event called "MakeSound" in the ReplicatedStorage.

Finally, I have a script in the ServerScriptService that looks like this:

local action = game:GetService("ReplicatedStorage"):WaitForChild("MakeSound")

player = game.Players.LocalPlayer
char = player.Character
humn = char.Humanoid
mouse = game.Players.LocalPlayer:GetMouse()

function KeyD(key)
	key = key:lower()
		if  key =="1" then
			local sound = Instance.new("Sound")
			sound.Volume = 1
			sound.SoundId = "rbxassetid://725011552"
			sound.Parent = char.Head
			sound:Play()
            wait(3)
			sound:Destroy()
		end
end

mouse.KeyDown:connect(KeyD)

My theory was that after the player clicks on the GUI, it allows for them to start making this sound by pressing 1, but the output tells me "attempt to index global 'player' (a nil value)" for Line 2.

As a beginner, I have no idea where to start problem solving. Any solutions? Please be descriptive!

1 answer

✓ Accepted answer

What I'm not understanding are two things. 1.) Why did you define the remote event and never connect a function to when the event actually fires, and 2.) Why would you define player when you can send that through the remote event. These are simple mistakes, especially if you are new to FE, but luckily they are easy to learn and/or fix. All you would need to do is add the word "player" inside the parenthesis after :FireServer(). Now instead of defining player in your local script, you will already have it as long as you also put the word "player" inside the parenthesis of the function we are calling next. That would make your local script look like this.

local action = game:GetService("ReplicatedStorage"):WaitForChild("MakeSound")

action.OnServerEvent:Connect(function(player)

player = game.Players.LocalPlayer
char = player.Character
humn = char.Humanoid
mouse = game.Players.LocalPlayer:GetMouse()

function KeyD(key)
    key = key:lower()
        if  key =="1" then
            local sound = Instance.new("Sound")
          	  sound.Volume = 1
          	  sound.SoundId = "rbxassetid://725011552"
          	  sound.Parent = char.Head
          	  sound:Play()
         	   wait(3)
          	  sound:Destroy()
      	 end
	end
end)

mouse.KeyDown:connect(KeyD)

Just that one line should make everything work fine. Hope I helped, but in the event that I didn't please leave a comment telling me the errors, or if there are none, what happened.

-Cmgtotalyawesome

EDIT: If you were still wondering why player came back undefined, it is simply because local player can only be accessed in a local script, if that is a local script, it can't be in ServerScriptService.

3 comments

0

Thank you so much, this was incredibly helpful. I wish I could upvote this! Thanks for being detailed and explaining everything. I am trying to learn as much as I can. However, when I followed the advice, I came across another error. "attempt to index global 'mouse' (a nil value)" for line 24. Shouldn't this not happen since the variable is defined above? Thanks for all your help

0

Okay, I got it to work, but only on Studio, not when i play it in game. It fires a remote event so I'm not sure why its not working in game.

0

Turns out it ended up working without a remote event at all! I took out the remote event and FireServer, and put the sound inside of a part on the player, and it works online and others can hear it. Thanks!

This discussion is preserved as it was. New answers, comments, and votes are closed.