How do I spawn something when a key is pressed

I’m trying to make it so that if you either press Q or J a banana peel appears in your current position, I’m experiencing some issues though.

local plr = game.Players.LocalPlayer
uis.InputBegan:Connect(function(keypress)
	if keypress.UserInputType.EnumType == Enum.KeyCode.J or keypress.UserInputType.EnumType == Enum.KeyCode.Q then
		local repli = game:GetService("ReplicatedStorage")

		repli.bananaPeel:FireServer(plr)
		
	end
end)

this is the local script

local replicatedstorage = game:GetService("ReplicatedStorage")
replicatedstorage.bananaPeel.OnServerEvent:Connect(function(player)
	local plr = game.Players:GetPlayerFromCharacter(player)
	local peel = storage.BananaPeel:Clone()
	peel.Parent = workspace
	peel.Position = plr.Character.RightLeg
end)
1 Like

Should be keypress.KeyCode that’s an issue

1 Like

anything else i can do, fixed this but it still doesnt spawn

Player already is the “plr” so remove that. Other than that any errors?

In the server script, put a print in the top and print the player, if anything at all prints then it’s an issue with your clone script, else it’s your key script.

This is likely the cause of the script not working. The argument passed to GetPlayerFromCharacter is the player object, not the character

Look who I found, a wild FrancoBritishAI. Hi.

replicatedstorage.bananaPeel.OnServerEvent:Connect(function(player)

As @domboss37 and @escapethepaw already said:
Whenever you call :FireServer(), the script automatically passes the player as an argument. So, when you’re doing function(player), that player already is the player. There is no need to define a plr variable, you can just do peel.Position = player.Character.RightLeg.

1 Like

I fixed your scripts for you just copy and paste these in.
Server Script

local replicatedstorage = game:GetService("ReplicatedStorage")
replicatedstorage.bananaPeel.OnServerEvent:Connect(function(player,char)
	
	local peel = game.ServerStorage:WaitForChild("BananaPeel"):Clone()
	peel.Parent = workspace
	peel.Position = char:WaitForChild("Right Leg").Position
end)

Local Script

local plr = game.Players.LocalPlayer
local Character = plr.Character
game:GetService("UserInputService").InputBegan:Connect(function(keypress)
	if keypress.KeyCode == Enum.KeyCode.J or keypress.KeyCode == Enum.KeyCode.Q then
		local repli = game:GetService("ReplicatedStorage")

		repli.bananaPeel:FireServer(Character)

	end
end)

Here’s a list of issues I found within your scripts:

1 In the local script you added the player in the FireServer function.
You don’t need to do this sense Roblox does this automatically.
2 You put UserInputType instead of Keycode. You wouldn’t need the type of input the users giving you sense you were only detecting if a certain key is pressed.
3 When you were setting the position of the peel you didn’t set it to the right leg’s position. When setting the position of a object to another you always need both objects positions.

There’s more I found but everyone else who contributed to this post already found them. I hope you learn from this experience and grow has a programmer!

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