I created this lengthy (and quite messy) LOCAL script for a shuttle craft. It makes a sound to simulate takeoff, and changes a decal in front of the window every so often to show it's moving. Then it grabs another shuttle from ReplicatedStorage and teleports you to a brick at the new shuttle. I tried putting it in a regular script, to find that this only works in studio. Here is the code:
01 | repeat wait() until game.Players.LocalPlayer |
02 | char = game.Players.LocalPlayer.Character |
03 | destination = game.Workspace.planshut |
04 |
05 | function onClick() |
06 | script.Parent.Sound:Play() |
07 | wait( 6 ) |
08 | script.Parent.Parent.view.Transparency = 0 -- view space |
09 | wait( 1 ) |
10 | script.Parent.Parent.view.Decal.Transparency = 0 |
11 | script.Parent.Parent.view.Decal.Texture = "http://www.roblox.com/asset?id=244339877" --view planet |
12 | wait( 1 ) |
13 | script.Parent.Parent.view.Decal.Texture = "http://www.roblox.com/asset?id=244339507" -- closer view |
14 | wait( 1 ) |
15 | script.Parent.Parent.view.Decal.Texture = "http://www.roblox.com/asset?id=244339544" -- surface view |
I can't work out what is wrong! It won't do anything, the output won't tell me anything at all either. Help?
When using LocalScripts, remember that LocalPlayer will always exist. No need to wait for it. You do, however, often need to wait for the character.
1 | local plr = game.Players.LocalPlayer |
2 | repeat wait() until plr.Character |
3 | local chr = plr.Character |
However, LocalScripts only run in PlayerGui or Backpack. They don't run in parts. So you must use a server Script.
But LocalPlayer only works in LocalScripts, so what do you do? Well it turns out that MouseClick events have a built in parameter equal to the Player who clicked. You can use that.
1 | function sayHi( plr ) |
2 | print (plr.Name.. " says hi!" ) |
3 | end |
4 | script.Parent.ClickDetector.MouseClick:connect( sayHi ) |
On a side note, you should really define some variables instead of using those long paths over and over.