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

How do I fix this script that only works in studio, but doesn't work in the actual game?

Asked by 6 years ago

This script is supposed to play a sound when touching a brick, while stopping other sounds

1   script.Parent.Touched:connect(function(hit)
2   if hit.Parent:FindFirstChild("Humanoid")then
3       game.Players[hit.Parent.Name].PlayerGui.Sound1.Volume = 1
4       game.Players[hit.Parent.Name].PlayerGui.Sound1:Play()
5       game.Players[hit.Parent.Name].PlayerGui.Sound2:Stop()
6   end
7   end)

However, when tested on roblox studio it works perfectly fine but when in the actual roblox game it fails to do anything.

I'm new to scripting so it's been a pain trying to figure out whats wrong with this script, please send help!

0
Don't put the sound in PlayerGui User#19524 175 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago
Problem Solution
:connect() is deprecated* use :Connect()
The server can't access objects in PlayerGui unless placed by the server Place the sound under the brick, or somewhere where the server is not limited to
Do not assume that an object with the same name as hit.Parent will be in the Players service Use GetPlayerFromCharacter
* Isn't what is causing the problem but you should still switch to the solution
local Players = game:GetService("Players")
local getPlayerFromCharacter

getPlayerFromCharacter = function(model)
    local plr = Players:GetPlayerFromCharacter(model)

    if plr then return plr end 
end

script.Parent.Touched:Connect(function(hit)
    local player = getPlayerFromCharacter(hit.Parent)

    if player then
        script.Parent.Sound1.Volume = 1
        script.Parent.Sound1:Play()
        script.Parent.Sound2:Play()
    end
end)

It also "worked" in studio because you used Play Solo mode. Play Solo is the client and server, not separated, but as one. Use local servers to test scripts instead.

0
thank you for the great response, got it figured now :) DannyKyun 14 — 6y
0
ok how would i make it so that the sound can only be heard locally, so that only one player can hear it not the entire server? DannyKyun 14 — 6y
Ad

Answer this question