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

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

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

✓ Accepted answer

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.

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