Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Trying to make a "part appear in front of player on Q" script, but its not working, any help?

Asked by 4 years ago

I'm relatively new to scripting, and I'm trying to make it so once the player presses "Q" on their keyboard, a part appears in front of them. For some reason, when I press Q, it only appears in the game, but not in front of the player, but rather at the center of the map (or the spawn point of the baseplate.) could anybody help? Here's the script I used.

local player = game.Players.LocalPlayer
local character = player.character
local mouse = player:GetMouse()
local enabled = true

mouse.KeyDown:Connect(function(key)
    if key == "q" then
    print("Pressed Q")
    local part = Instance.new("Part", game.Workspace)
    part.CanCollide = true
    part.newCFrame = character.Torso.CFrame*CFrame.new(0,0,-5)
    end
end)

2 answers

Log in to vote
1
Answered by
ScuffedAI 435 Moderation Voter
4 years ago
--Line 11 is suppose to be
part.CFrame = character.Torso.CFrame*CFrame.new(0,0,-5)

-- instead of
part.newCFrame = character.Torso.CFrame*CFrame.new(0,0,-5)
Ad
Log in to vote
0
Answered by 4 years ago
local player = game:GetService'Players'.LocalPlayer--I used getservice so that errors can't happen when game.Players is renamed
local enabled = true

game:GetService'UserInputService'.InputBegan:Connect(function(key,gps)
    if key.KeyCode == Enum.KeyCode.Q and gps==false and enabled == true then--when gps is false, the player is not typing in a GUI.
        print("Pressed Q")
        if player.Character and player.Character:FindFirstChild("Head") then --if the character exists with the head then
            local part = Instance.new("Part",workspace)--use workspace, game.Workspace has slight risks and workspace is shorter.
            part.CanCollide = true
            part.CFrame = player.Character.Head.CFrame+player.Character.Head.CFrame.LookVector*5--Makes part appear in front of player
        end
    end
end)
--make sure player.Character is capitalized, Character not character.

Notes: mouse.KeyDown is depricated so use UserInputService. Please use workspace instead of game.Workspace as workspace is shorter and workspace will work whether it's renamed or not.

Answer this question