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

Why doesnt anything spawn when I press the right key?

Im trying to make a block spawn infront of the player. So basically I want to use the keyboard to make some sort of spell and shortcut. I was wondering why it doesn't work please help. Problem is with the code because when i press it nothing spawns

local Players = game:GetService("Players")

local UIS = game:GetService("UserInputService")

UIS.InputBegan:Connect(function(input)

if input.UserInputType == Enum.UserInputType.Keyboard then -- Checks if the input was related to a keyboard

if input.KeyCode == Enum.KeyCode.Q then

if Players.LocalPlayer.Character then

local torso = Players.LocalPlayer.Character:WaitForChild("Torso")

local Part = Instance.new("Part")

local NewPart = Part:Clone()

NewPart.Parent = torso

NewPart.CFrame = torso.CFrame + torso.CFrame.LookVector * 10 --10 is the distance, in studs NewPart will be away from torso

game:GetService("Debris"):AddItem(NewPart,3)

end

end

end

end)

8 comments

0

Do you use R15 when testing this script? “Torso” can only be found in R6 characters, so try “HumanoidRootPart” instead. Also, creating a part then cloning it really isn’t necessary, just use the part you created instead of cloning it.

0

Im using R6 characters

0

Okay...is this a local script or a server (normal) script? Also, where is the script located?

0

Server script and its located in workspace

0

Oh, that’s why. Server scripts can’t use LocalPlayer and other local stuff like UserInputService. You must do that in a local script, and the local script has to be placed in certain places like StarterGui for it to work. You could copy all your code into a local script and it should work, but it would only show for you I think.

0

By the way, if you want the part to show for everyone, you would have to use remote events. Here’s a tutorial on how to use it : https://developer.roblox.com/articles/Remote-Functions-and-Events

0

Changed it to a local doesnt work

0

Yeah, local scripts won't run in the Workspace.

1 answer

✓ Accepted answer

Okay, so I tested your code and everything works fine for me. Make sure it's a local script though in StarterGui that's not disabled. There's also some formatting errors in your code, so I decided to fix that up for you.

Here's the code :

local Players = game:GetService("Players")

local UIS = game:GetService("UserInputService")

UIS.InputBegan:Connect(function(input)
    if input.UserInputType == Enum.UserInputType.Keyboard then -- Checks if the input was related to a keyboard
	    if input.KeyCode == Enum.KeyCode.Q then
		    if Players.LocalPlayer.Character then
		    local torso = Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart")
		    local Part = Instance.new("Part")
		    Part.Parent = torso
		    Part.CFrame = torso.CFrame + torso.CFrame.LookVector * 10 --10 is the distance, in studs NewPart will be away from torso
		    game:GetService("Debris"):AddItem(Part,3)
		    end
	    end
    end
end)

The part may only be visible for you since this script works clientsided. If you want it to show for everyone, you can use remote events.

If you have any more questions, please comment!

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