Why is my script not working?

Hello, today I was making a plinko game, but I stumbled upon this issue.
Basically all I want my script to do is to drop a ball in a plinko chamber with the spacebar or retun key (better known as enter)

I have tried all what I could really think of and nothing has really worked out.

I think you guys could give it a try and maybe help me out and tell me what the issue is.

Note: I am not good at coding, lol, I just do it for fun and I don’t have any goals.
image

This is my code

local UserInputService = game:GetService("UserInputService")

UserInputService.InputBegan:Connect(function(Key) -- Input detected!
	if Key.KeyCode == Enum.KeyCode.Space or Enum.KeyCode.Return  -- Checking if input is 'E'
	then
		local sphere = Instance.New("Part")
		sphere.Size = Vector3.new(1.1,1.1,1.1) -- Put whatever size you want the part to be here in studs (X,Y,Z)
		sphere.Shape = Enum.PartType.Ball
		sphere.Color = Color3.new(0.803922, 0.329412, 0.294118)
		sphere.Position = Vector3.new(-9.486, 37.43, 19.383) -- Put the position of where you want the block to spawn
		sphere.Anchored = false
		sphere.CanCollide = true
		sphere.Parent = workspace
	end
end)

	UserInputService.InputEnded:Connect(function()
	print("The Part Has Successfully Been Dropped")
end)

At first glance the code looks fine, but make sure this is contained in a LocalScript either in StarterGui, the Player, the Character, or somewhere else on the client-side of the network. UserInputService only works on the client environment.
Now, if you want this to replicate to other players that’s going to require using a remote event, but if your goal is to just get a ball to appear, you’ll be fine just doing what I said in the previous paragraph.

Also, this line:
local sphere = Instance.New("Part")
should have a lowercase “new”

Alright thank you for the response, ill see if it works.

image
Thank you for your solution! Worked but, each time a key on my keyboard is pressed, a ball spawns in. That’s not supposed to happen. Only the Return key and Space should trigger the script. Do you have a solution to that?

Add a debounce (charrrrrrrr limit)

This is the problem, you need to do your check each time to compare the keycode to the keycode you want. This should work:

if Key.KeyCode == Enum.KeyCode.Space or Key.KeyCode == Enum.KeyCode.Return  -- Checking if input is 'E'
	then

If you just say or Enum.KeyCode.Return without Key.KeyCode == Enum.KeyCode.Return then what you are doing is just checking if “Enum.KeyCode.Return” exists, which is always true. So no matter what button you pressed, the event would fire and this would be “true”, and spawn a ball. With the change above, this should now compare the key you pressed to the return key to make sure only space and enter spawn a ball.

1 Like

On line 4 you have a comment saying "Checking if input is ‘E’. "

I believe you would have to do this instead

if Key.KeyCode == Enum.KeyCode.E then

Since this is UIS (userinputservice) i believe there is this thing where if you let go of said key it will read the input and run the code again so just to be safe do this:

if Key.UserInputState ~= Enum.UserInputState.Begin then return end

Thank you, this worked out great. I am still unsure how to make the ball register in the boxes down below, do you think it’s possible?

That will be a bit trickier, since we currently are spawning the ball on the client. I’d recommend making a new post if you need additional help with this as the topic has changed a bit, since now you need to work on Replication.
Read the wiki about it here: Client-Server Model | Roblox Creator Documentation
You’ll need a Server script (regular script) and a Local script. The local script should handle the button pressing, and should tell the server via a Remote Event to spawn a part. The server should spawn the part, and have a check for each box via something like the .Touched event of a part. Hope that helps get you started!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.