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

After the player dies, the script stops working?

Asked by 8 years ago

This script basically is a new controlscript for the character. Instead of moving to where the player's camera is facing, it moves them to the corresponding direction of the button pressed (w = up, a = left, etc.) This script only works once, then stops after that with no error. I think it has something to do with the moveto because all the prints are working fine,

X = 3
W = -3
Q = Vector3.new(3,0,0)
E = Vector3.new(-3,0,0)

game:GetService("RunService").RenderStepped:connect(function()
local Input = game:GetService("UserInputService")

if Input:IsKeyDown(Enum.KeyCode.W) and game.Players.LocalPlayer.Character then
game.Players.LocalPlayer.Character.Humanoid:MoveTo(game.Players.LocalPlayer.Character.Torso.Position + (game.Players.LocalPlayer.Character.Torso.CFrame.lookVector * X))
print(game.Players.LocalPlayer.Character) end
if Input:IsKeyDown(Enum.KeyCode.A) and game.Players.LocalPlayer.Character then
game.Players.LocalPlayer.Character.Humanoid:MoveTo(game.Players.LocalPlayer.Character.Torso.CFrame * E)
print(game.Players.LocalPlayer.Character) end
if Input:IsKeyDown(Enum.KeyCode.S) and game.Players.LocalPlayer.Character then
game.Players.LocalPlayer.Character.Humanoid:MoveTo(game.Players.LocalPlayer.Character.Torso.Position + (game.Players.LocalPlayer.Character.Torso.CFrame.lookVector * W))
print(game.Players.LocalPlayer.Character) end
if Input:IsKeyDown(Enum.KeyCode.D) and game.Players.LocalPlayer.Character then
game.Players.LocalPlayer.Character.Humanoid:MoveTo(game.Players.LocalPlayer.Character.Torso.CFrame * Q)
print(game.Players.LocalPlayer.Character) end
end)

(sorry for the messy script)

1 answer

Log in to vote
0
Answered by
BlackJPI 2658 Snack Break Moderation Voter Community Moderator
8 years ago

First of all, I'm not sure why you'd want to check to see if the player is touching a button every single render step. It is much more efficient to keep a list of each key's state and update them when each key is pressed or released.

Secondly, I'd try using :Move() instead of :MoveTo(). :Move() will make the player move in the given direction, and comes equipped with a bool for if the movement should be relative to the camera or not.

Lastly the reason your code stops working is because when the character dies, game.Player.LocalPlayer.Character is nil for a bit while you respawn. This makes an error pop in your script.

The following Code is an example of what I would possibly do in order to accomplish what you are trying to. Note however the initiation of the Character variable will no longer be valid upon the first respawn and the gameProcessed variable simply means the player's focus is on the game (not chatting or typing in a textbox, etc).

Leave a comment if you need anything explained further.

local UserInputService = game:GetService("UserInputService")
local Character = game.Players.LocalPlayer.Character
local Keys = {}

function UpdateMovement()
    local forwards = Keys[Enum.KeyCode.W] or Keys[Enum.KeyCode.Up] and true or false
    local backwards = Keys[Enum.KeyCode.S] or Keys[Enum.KeyCode.Down] and not forwards and true or false
    local left = Keys[Enum.KeyCode.A] or Keys[Enum.KeyCode.Left] and true or false
    local right = Keys[Enum.KeyCode.D] or Keys[Enum.KeyCode.Right] and not left and true or false
    local xMovement = left and -1 or right and 1 or 0
    local zMovement = forwards and -1 or backwards and 1 or 0
    Character.Humanoid:Move(Vector3.new(xMovement, 0, zMovement), false)
end

UserInputService.InputBegan:connect(function(input, gameProcessed)
    if input.UserInputType == Enum.UserInputType.Keyboard and not gameProcessed then
        Keys[input.KeyCode] = true
        UpdateMovement()
    end
end)

UserInputService.InputEnded:connect(function(input, gameProcessed)
    if input.UserInputType == Enum.UserInputType.Keyboard then
        Keys[input.KeyCode] = false
        UpdateMovement()
    end
end)

Reinitalize

The easiest way to do this is to disable then re-enable the control script forcing it to restart.

Server Script

game.Players.PlayerAdded:connect(function(player)
    player.CharacterAdded:connect(function(character)
        CONTROLSCRIPTLOCATION.Enabled = true
        character.Humanoid.Died:connect(function()
            CONTROLSCRIPTLOCATION.Enabled = false
            character.Humanoid.Died:disconnect()
        end)
    end)
end)
0
This still does not work after the player dies and returns no error. HungryJaffer 1246 — 8y
0
Did you read my note? You have to write a funtion to re initalize the character when after it dies and respawns. BlackJPI 2658 — 8y
0
See the update for an example on one way to do this BlackJPI 2658 — 8y
Ad

Answer this question