Key Pressing Script Problem

I’m trying to have a player press a key at a certain time and when they press it on time, it adds + 1 to a value. It doesn’t work, but when I look at the output, it says that there are 0 errors. No way for me to try and find the problem.

There are 2 scripts, one in server script service and another in workspace

Workspace Script:

local Replicated = game:GetService("ReplicatedStorage")
local players = game:GetService("Players")
local Player = players.LocalPlayer
local workspace = game:GetService("Workspace")
local Uis = game:GetService("UserInputService")


local function onTouched(hit)
	workspace:WaitForChild("DrinkPart").bg.g.Enabled = true
	task.wait(0.5)
	workspace:WaitForChild("DrinkPart").bg.g.Enabled = false
end

local connection = script.Parent.Touched:connect(onTouched)

Uis.InputBegan:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.F then
		if workspace:WaitForChild("DrinkPart").bg.g.Enabled == true then
			Replicated:WaitForChild("Score"):FireServer()
		end
	end
end)

Server Script Service script:

local Replicated = game:GetService("ReplicatedStorage")

local players = game:GetService("Players")

local Player = players.LocalPlayer

Replicated:FindFirstChild("Score").OnServerEvent:Connect(function(Player)

Player.leaderstats.BottlesDrank.Value += 1

end) 

I also have a Remote Event called Score.

And just some additional information, I went on the server side while testing the game to see what would happen if I tried to do it there, and obviously it didn’t work since you cannot fire remote events from there but, it recognized that I actually pressed the key and got to the line where it would’ve fired the remote event. This is weird because it does not even recognize it when I am on client side.

You should be keeping the local script in StarterPlayerScripts, not in the workspace. Change that and adjust the pathways appropriately. Also with your script in ServerScriptService you can’t define player like that because it’s not a local script

1 Like

I tried it, and got an error saying that "Touched is not a valid member of PlayerScripts “Players.infinityTNT.PlayerScripts” Any workarounds for that?

That’s what I meant by change the pathways. You’d have to make a path to where the part is in the workspace

Oh, sorry my fault I didn’t see that. I fixed it and well the script doesn’t even work at all anyways now, the Gui’s don’t even show up or anything. Still no errors though.

Here’s what my explorer bar looks like.

Drawing (14)

Hmm is the bg.g a buildboard GUI?

Yeah. That’s the billboard Gui.

Here is the current script in Starter Player Scripts by the way

Drawing (15)

Weird. I replicated what you have and it seems to work for me. Add a print(“This works”) in the onTouched function and see if that fires, and you can add a print in the InputBegan function too to see if that is working.

Drawing (16)
The Output shows that it works, but not in the way I want it to. In the script I want the player to only be able to press the key when the gui is shown, and when it isn’t showing, then it won’t work. And when the player does press the key when the gui is shown, then it will add +1 to a leaderstat. None of that happened when I tested it. And even though the On Touched print shows, the gui does NOT show, its still invisible.

I started a studio test and left it open when I typed this. After typing this message, I went back into studio, and it started working (yay) so I stopped it and restarted it to see what would happen and it didn’t work again until about a couple minutes after. I don’t know what is happening, maybe it only works after a certain time???

This is what I have done. From the looks of it, it’s basically the same as yours. The issue might be with your GUIs then, so maybe include a picture showing what they look like in the explore tab

Oh my gosh. I just realized I never said what was supposed to be touching the part. I don’t want a player to touch a part, I am making a Model touch a part. I have a part that copies a Model out of replicated storage and puts it on a conveyor, and on the conveyor is another part, that when the Model touches it, it should bring up the gui and everything.

Here is what I was trying to accomplish. When the bottle hits the part in the middle, it should bring up the gui and allow the player to press the F key and add a point to the leaderstats.

Ohh then this will probably need to be done different. Instead of having the touched event be in a local script move it back to a server script but keep the InputBegan function in the local script. And that server script can be kept in the part that is detecting the touching.

And just to be clear, when the bottles get cloned that is being done from a server script, correct?

The bottles get cloned in a script in workspace. Should I move it to a Server Script? and I will do all the changes you stated here.

Okay yeah thats how they should be cloned. And yeah just move this:

local function onTouched(hit)
	workspace:WaitForChild("DrinkPart").bg.g.Enabled = true
	task.wait(0.5)
	workspace:WaitForChild("DrinkPart").bg.g.Enabled = false
end

local connection = script.Parent.Touched:connect(onTouched)

into a server script inside that part. Leave the rest in the local script

So I move that inside a part on a server script?

Yeah and then let me know how it goes. And if it doesn’t work please include what the GUI looks like in the explorer panel

Tested it, GUI shows up fine, but the Key pressing does not work. I don’t get that print message in the output when I press the key at the right time or any time, and no score is being added to my leaderstats.

Okay thats good, and yeah with the way the server script is it won’t award anything. Change the script to look like this

local Replicated = game:GetService("ReplicatedStorage")

Replicated:FindFirstChild("Score").OnServerEvent:Connect(function(Player)

Player.leaderstats.BottlesDrank.Value += 1

end) 

See if that adds the point