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

How do I use StarterCharacterScripts?

Asked by 8 years ago
I'm trying to learn how to use ROBLOX's StarterChearacterScripts, but I can't get any script I use to work. I've been using a LocalScript instead of a regular Script. 
-- This is a code I attempted, but failed. I'm new to StarterCharacterScripts, and could use a great example on how to use them.

local PlayersService = game:GetService('Players')

local function Selector()
    local player = PlayersService.LocalPlayer
    local character = player.character
    character.Humanoid.WalkSpeed = 0
end

PlayersService.LocalPlayer.CharacterAdded:connect(Selector)

1 answer

Log in to vote
1
Answered by 8 years ago

Most of the script above is unnecessary because of how StarterCharacterScripts works.

The basic concept behind StarterCharacterScripts is that any descendant of it will get parented to a player's character whenever it spawns. Subsequently, once the character dies, the script is destroyed and parented again as soon as the player's character spawns.

This means we could boil down your code to the following:

local Players = game:GetService("Players")

local Player = Player.LocalPlayer
local Character = Player.Character
Character:WaitForChild("Humanoid").WalkSpeed = 0

What does this mean for you? Well, once the player spawns, the Local Script inside of StarterCharacterScripts will be cloned, and parented to the character. Once that happens, the script will run as a normal script. The humanoid will soon have it's walkspeed set to 0.

You might note I also added a small piece of code, namely the function WaitForChild. When the script is parented to the player, he/she might not have yet fully loaded, so it's always a best practice to check if the object (in this case "Humanoid") is already present.

Good luck, and enjoy coding!

Ad

Answer this question