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

Why doesn't this local script work?

Asked by 9 years ago

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
0
Is this your whole script? If not, please give the whole script so we can help. alphawolvess 1784 — 9y
0
Its the whole script DeveloperSolo 370 — 9y

2 answers

Log in to vote
0
Answered by 9 years ago

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.

0
You shouldn't use KeyDown any more. That is deprecated and is suggested to use UserInputService. alphawolvess 1784 — 9y
0
Thanks :) DeveloperSolo 370 — 9y
0
Well, they both work. DeveloperSolo 370 — 9y
0
You can do a lot more with UserInputService and you never know when ROBLOX could decide to end the functinality of KeyDown, if they do. It's highly suggested to learn UserInputService! alphawolvess 1784 — 9y
0
I wouldn't say "shouldn't". UserInputService was simply created to make user input compatibility possible on multiple platforms. I highly doubt they'd trash an event that's used in thousands of games, when it has the same functionality without any error-causing effects. However, I would suggest getting into UserInputService simply because of their new dynamics. ScriptGuider 5640 — 9y
Ad
Log in to vote
1
Answered by 9 years ago

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).

  • Okay, now lets put this all together with your script!
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!

Answer this question