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

Script (Teleport, WalkSpeed, and Sound:Play() ) works in Studio but not in real game?

Asked by
MexheCo 46
6 years ago

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:

script.Parent.Touched:connect(function(hit) 
 if hit.Parent:FindFirstChild('Humanoid') then 

  hit.Parent.Head.CFrame = CFrame.new(-736.004, 8.161, 124.424)
  game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 16
  game.Workspace.WorkspaceSoundService.Sound:Play()

 end
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?

1 answer

Log in to vote
1
Answered by 6 years ago
Edited 6 years ago

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.

script.Parent.Touched:Connect(function(hit)  --Use :Connect(), :connect() is deprecated.
 if hit.Parent:FindFirstChild('Humanoid') and game.Players:GetPlayerFromCharacter(hit.Parent) then 

  hit.Parent.Head.CFrame = CFrame.new(-736.004, 8.161, 124.424)
  hit.Parent.Humanoid.WalkSpeed = 16
  game.Workspace.WorkspaceSoundService.Sound:Play()

 end
end)

You may need to use a RemoteEvent if you want the sound to play on the server.

Ad

Answer this question