Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

I can't work out what's wrong?

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:

repeat wait() until game.Players.LocalPlayer
char = game.Players.LocalPlayer.Character
destination = game.Workspace.planshut

function onClick()
	script.Parent.Sound:Play()
	wait(6)
	script.Parent.Parent.view.Transparency = 0 -- view space
	wait(1)
	script.Parent.Parent.view.Decal.Transparency = 0
	script.Parent.Parent.view.Decal.Texture = "http://www.roblox.com/asset?id=244339877" --view planet
	wait(1)
	script.Parent.Parent.view.Decal.Texture = "http://www.roblox.com/asset?id=244339507" -- closer view
	wait(1)
	script.Parent.Parent.view.Decal.Texture = "http://www.roblox.com/asset?id=244339544" -- surface view
	wait(1)
	local s = game.ReplicatedStorage.planetshut:clone()
	s.Parent = game.Workspace -- make shuttle on planet
	if script.Parent.Parent.Door.Transparency == 0 then
		s.Door.Transparency = 0
		s.Door.CanCollide = true
	else
		s.Door.Transparency = 1
		s.Door.CanCollide = false
	end --if door is open, open door on planet shuttle
	char.Torso.Anchored = true
	char.Torso.CFrame = destination.CFrame + Vector3.new(0,4,0)
	wait(0.2)
	char.Torso.Anchored = false --teleport player to planet shuttle
	script.Parent.Parent:Destroy() --destroy this shuttle
end

script.Parent.ClickDetector.MouseClick:connect(onClick)

I can't work out what is wrong! It won't do anything, the output won't tell me anything at all either. Help?

1 answer

✓ Accepted answer

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.

local plr = game.Players.LocalPlayer
	repeat wait() until plr.Character
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.

function sayHi( plr )
	print(plr.Name.." says hi!")
end
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.

2 comments

0

I changed the script according to this, and the output tells me "Torso is not a valid member of Player"

0

Like I said, the parameter is equal to the PLAYER who clicked it. It's NOT equal to a Character. You therefore need to use the Player's Character property, e.g., plr.Character

This discussion is preserved as it was. New answers, comments, and votes are closed.