I have a script that does three things:
When touched, teleports the player
When Touched, plays a sound
And when touched, sets the player's WalkSpeed to 16
Here is the Script:
1 | script.Parent.Touched:connect( function (hit) |
2 | if hit.Parent:FindFirstChild( 'Humanoid' ) then |
3 |
4 | hit.Parent.Head.CFrame = CFrame.new(- 736.004 , 8.161 , 124.424 ) |
5 | game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 16 |
6 | game.Workspace.WorkspaceSoundService.Sound:Play() |
7 |
8 | end |
9 | end ) |
Everything works fine in Studio, but when I get into the actual game, it ignores the sound and WalkSpeed changes, and just teleports.
Any help?
A script cannot access LocalPlayer; it'll just return nil.
Instead, you should use game.Players:GetPlayerFromCharacter(hit.Parent) instead of game.Players.LocalPlayer. Since you aren't actually using the Player object, it's best to check if the character belongs to a player.
1 | script.Parent.Touched:Connect( function (hit) --Use :Connect(), :connect() is deprecated. |
2 | if hit.Parent:FindFirstChild( 'Humanoid' ) and game.Players:GetPlayerFromCharacter(hit.Parent) then |
3 |
4 | hit.Parent.Head.CFrame = CFrame.new(- 736.004 , 8.161 , 124.424 ) |
5 | hit.Parent.Humanoid.WalkSpeed = 16 |
6 | game.Workspace.WorkspaceSoundService.Sound:Play() |
7 |
8 | end |
9 | end ) |
You may need to use a RemoteEvent if you want the sound to play on the server.