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

How do I get a tool to spawn a brick in front of the player when a key is pressed?

I'm trying to make a revolver that upon pressing 'e', a brick appears. I've been looking for a while, and I came across multiple people saying that in order to find the Torso of a character, it needs to be a local script. How can I make bricks spawn in front of the player without being client sided?

Basically, this is what I have so far. (The Local script.)

local tool = script.Parent
local UserInputService = game:GetService("UserInputService")
local connection
local player = game.Players.LocalPlayer


local function Connect()
	connection = UserInputService.InputBegan:Connect(function(input, gp)
		if gp then return end

		if input.KeyCode == Enum.KeyCode.E then 
			script.RemoteEvent:FireServer()
			end
		
	end)
end

	

local function Disconnect()
	connection:Disconnect()
end

tool.Equipped:Connect(Connect)
tool.Unequipped:Connect(Disconnect)

(The Regular script.)

local player = game.Players.LocalPlayer
local wepen = script.Parent.Parent.Parent
script.Parent.OnServerEvent:Connect(function(Player)
	local brick = Instance.new("Part",workspace)
	brick.Parent = workspace
	brick.CFrame = wepen.CFrame*CFrame.new(0,0,-5)
	wait(3)
	brick.Transparency = 0.1
	wait(0.05)
	brick.Transparency = 0.2
	wait(0.05)
	brick.Transparency = 0.3
	wait(0.05)
	brick.Transparency = 0.4
	wait(0.05)
	brick.Transparency = 0.5
	wait(0.05)
	brick.Transparency = 0.6
	wait(0.05)
	brick.Transparency = 0.7
	wait(0.05)
	brick.Transparency = 0.8
	wait(0.05)
	brick.Transparency = 0.9
	wait(0.05)
	brick.Transparency = 1
	wait(1)
	brick:Destroy()

end) 

Anytime I at least try to find the Torso, I always get an error.

1 comment

0

On Line 1 of your "Regular Script", which I am assuming is a Script, is trying to find the LocalPlayer, which is what only LocalScripts can do.

2 answers

✓ Accepted answer

The detection of pressing e:


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

mouse.KeyDown:connect(function(key)
    if key == "e" then
        print("Pressed e")
    end
end)

The Plain old script:


local Players = game:GetService(“Players”)
local wepen = script.Parent.Parent.Parent
script.Parent.OnServerEvent:Connect(function(Player)
    local brick = Instance.new("Part",workspace)
    brick.Parent = workspace
    brick.CFrame = wepen.CFrame*CFrame.new(0,0,-5)
    wait(3)
    brick.Transparency = 0.1
    wait(0.05)
    brick.Transparency = 0.2
    wait(0.05)
    brick.Transparency = 0.3
    wait(0.05)
    brick.Transparency = 0.4
    wait(0.05)
    brick.Transparency = 0.5
    wait(0.05)
    brick.Transparency = 0.6
    wait(0.05)
    brick.Transparency = 0.7
    wait(0.05)
    brick.Transparency = 0.8
    wait(0.05)
    brick.Transparency = 0.9
    wait(0.05)
    brick.Transparency = 1
    wait(1)
    brick:Destroy()

end) 

1 comment

0

thank you, it worked!

maybe try changing this to the regular script

script.Parent.OnServerEvent:Connect(function(Player)
    local brick = Instance.new("Part",workspace)
    brick.Parent = workspace
    brick.CFrame = Player.Character.Torso.CFrame*CFrame.new(0,0,-5)
    wait(3)
    brick.Transparency = 0.1
    wait(0.05)
    brick.Transparency = 0.2
    wait(0.05)
    brick.Transparency = 0.3
    wait(0.05)
    brick.Transparency = 0.4
    wait(0.05)
    brick.Transparency = 0.5
    wait(0.05)
    brick.Transparency = 0.6
    wait(0.05)
    brick.Transparency = 0.7
    wait(0.05)
    brick.Transparency = 0.8
    wait(0.05)
    brick.Transparency = 0.9
    wait(0.05)
    brick.Transparency = 1
    wait(4)
    brick:Destroy()

end) 

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