So I'm trying to make a script where rocks come out of you when you press "v".
There is no error, and I have no idea whats wrong.
The local script is in the players backpack
code:
Player = game.Players.LocalPlayer Char = Player.Character Torso = Char.Torso Mouse = Player:GetMouse() function EarthKick(key) key = key:lower() if key == "v" then for i = 1, 100, 3 do local EarthBlock = Instance.new("Part") EarthBlock.Parent = workspace EarthBlock.Size = Vector3.new(math.random(4, 6),math.random(4, 6),math.random(4, 6)) EarthBlock.Anchored = true EarthBlock.TopSurface = 0 EarthBlock.BottomSurface = 0 EarthBlock.BrickColor = BrickColor.new("Brown") EarthBlock.CFrame = CFrame.new(Torso.Position.x,5,Torso.Position.z) * CFrame.new(0,0,-1) * CFrame.Angles(math.random(),math.random(),math.random()) end end end
A few things wrong here.
1: Player character
When using local scripts, or any script at all that's running as soon as the client has registered in the game, it's wise to 'wait' for the character to load, before trying to index it. We can do this by attaching the wait function, to the 'CharacterAdded' event of the player, and have a variable to store the result. Like this:
local Player = game.Players.LocalPlayer local Char = Player.CharacterAdded:wait()
Now, obviously this won't be the case for every situation. Sometimes the character loads on time, and the CharacterAdded event is redundant. But since there's no guarantee, we can use an 'or' statement to settle the dispute:
local Player = game.Players.LocalPlayer local Char = Player.Character or Player.CharacterAdded:wait()
2: Your function
Nothing seems to be wrong with the code in your function, however there's one simple thing you forgot to do; which is binding it to a mouse event. The mouse event you're most likely looking for us 'KeyDown', which fires whenever a key is pressed on your keyboard (obviously). All you have to do is use the 'connect' method with this event, and return your function as an argument. Finally, this is what the final result should look like:
local Player = game.Players.LocalPlayer local Char = Player.Character or Player.CharacterAdded:wait() local Torso = Char:WaitForChild("Torso") -- wait for the torso local Mouse = Player:GetMouse() local function EarthKick(key) key = key:lower() if key == "v" then for i = 1, 100, 3 do local EarthBlock = Instance.new("Part") EarthBlock.Parent = workspace EarthBlock.Size = Vector3.new(math.random(4, 6),math.random(4, 6),math.random(4, 6)) EarthBlock.Anchored = true EarthBlock.TopSurface = 0 EarthBlock.BottomSurface = 0 EarthBlock.BrickColor = BrickColor.new("Brown") EarthBlock.CFrame = CFrame.new(Torso.Position.x,5,Torso.Position.z) * CFrame.new(0,0,-1) * CFrame.Angles(math.random(),math.random(),math.random()) end end end -- 'KeyDown' is the event, 'EarthKick' is the function that will be called when the event is fired. Mouse.KeyDown:connect(EarthKick)
Hope this helped.
I'm not sure how you came up with the idea of you function detecting what key the player is pressing, this should not detect anything which then causes nothing to happen.
You need to use the UserInputService
which can tell you what key the player has pressed (Along with some other useful information)
A good way to set this up is to make a variable of the service, like so:
local InputService = game:GetService("UserInputService") -- Variable can be anything, like usual
Now how can we begin to use this Service to obtain information on which key the player is pressing? We would use InputBegan
(InputService.InputBegan:connect(function(Pressed)) Pressed is the argument which will return the key you've pressed (among other things beside keyboard input).
local Player = game.Players.LocalPlayer repeat wait() until Player.Character -- This will wait until the player's character exists so we get no errors local Char = Player.Character local Torso = Char:WaitForChild("Torso") -- Just incase Torso is not yet loaded local Mouse = Player:GetMouse() local InputService = game:GetService("UserInputService") InputService.InputBegan:connect(function(Pressed) if Pressed.KeyCode == Enum.KeyCode.V then -- You could use key, but I prefer to use Pressed. for i = 1, 100, 3 do local EarthBlock = Instance.new("Part") EarthBlock.Parent = workspace EarthBlock.Size = Vector3.new(math.random(4, 6),math.random(4, 6),math.random(4, 6)) EarthBlock.Anchored = true EarthBlock.TopSurface = 0 EarthBlock.BottomSurface = 0 EarthBlock.BrickColor = BrickColor.new("Brown") EarthBlock.CFrame = CFrame.new(Torso.Position.x,5,Torso.Position.z) * CFrame.new(0,0,-1) * CFrame.Angles(math.random(),math.random(),math.random()) end end end)
Comment any questions. If this helps, please accept this answer!