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!
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.