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

Why doesn't this script make me levitate and then move to my target location?

Asked by
Mystdar 352 Moderation Voter
8 years ago

The code is for some rocket boots but they are not currently working, this is my local script in the starterGui:

player = game.Players.LocalPlayer
spacedown = false
mouse = player:GetMouse()
startedDown = 0
character = player.character

mouse.KeyDown:connect(function(key)
    if key:lower() == " " then
        spacedown = true
        startedDown = tick()
    end
end)

mouse.KeyUp:connect(function(key)
    if key:up() == " " then
        spacedown = false
    end
end)

function Elevate()
    if character["Left Leg"]:findFirstChild("Rocketboot") ~= nil and character["Right Leg"]:findFirstChild("Rocketboot") ~= nil then
        if spacedown then
            print("Was held for ", tick() - startedDown,"seconds")
            -- Time between now and when `startedDown` was set
            if tick() - startedDown > 2 then -- If held for longer than 2 seconds
                local float = Instance.new("BodyVelocity", character.Torso).force 
                float.Parent.name = "float"
                float = Vector3(0, 196.2*character.Torso:GetMass(), 0) -- Apply force to make the character levitate
                local body = Instance.new("BodyVelocity",character.Torso) --Create BodyVelocity and Parents it to Player's Torso.
                body.name = "Body"              
                body.maxForce = Vector3.new(1e7,1e7,1e7)--The the Maximum amount of force the BodyVelocity can use.
                body.velocity = character.Torso.CFrame.lookVector*40*2--Change the Body's Velocity to the way the Character's Head if facing.
                character["Left Leg"]:findFirstChild("Rocketboot").Thrust1.Transparency = 0.5
                character["Right Leg"]:findFirstChild("Rocketboot").Thrust2.Transparency = 0.5
            end
        elseif not spacedown then
            character.Torso.float:remove()
            character.Torso.body:remove()
            character["Left Leg"]:findFirstChild("Rocketboot").Thrust1.Transparency = 1
            character["Right Leg"]:findFirstChild("Rocketboot").Thrust2.Transparency = 1
        end
    end
end

spacedown.Changed:connect(Elevate)

Please can you tell me of any point in which my code is inefficient or could use improvement, currently the error is on line 5:

11:07:11.442 - character is not a valid member of Player 11:07:11.443 - Script 'Players.Player.PlayerGui.LocalScript', Line 5 11:07:11.444 - Stack End

1 answer

Log in to vote
1
Answered by 8 years ago

Remember Lua is case sensitive which means that you'll need change character = player.character to character = player.Character! This is the reason why you had an Error saying:

character is not a valid member of Player

Also remove() is deprecated so use Destroy() whenever possible and try and use UserInputService when dealing with the User's device input.

If you have anymore question/comment, please feel free to leave a comment down below.

-UserOnly16Characters

Ad

Answer this question