Is there something wrong with my code? I already tried to get help from other places before, they programmed in the dame thing as me, and I’m the one who gets the error. This is the code:
local Player = game.Players.LocalPlayer
script.Parent.Touched:connect(function(hit)
local PlrGui = Player.PlayerGui
PlrGui:FindFirstChild("Sound")
PlrGui.Sound:Destroy()
end)
Maybe, try to 1st print the PlayerGui, if it says something like: PlayerGui.
Add a script, beneath the script (must be a localscript!).
local Player = game.Players:FindFirstChildOfClass("Player")
script.Parent.Touched:connect(function()
script.LocalScript.Enabled = true and script.Enabled == false -- Why is there "==" on script.Enabled? Because when you do only once it'll error in the script analysis, automatically, it'll error on the output.
end)
On this localscript, put
local Player = game.Players.LocalPlayer
local PlrGui = Player.PlayerGui
PlrGui:WaitForChild("Sound")
PlrGui.Sound:Destroy()
script.Parent.Enabled = true and script.Enabled == false -- Why is there "==" on script.Enabled? Because when you do only once it'll error in the script analysis, automatically, it'll error on the output.
Also, put it on #help-and-feedback:scripting-support, it’s the main topic subject that contains errors on the output/no errors but doesn’t function at all.
You need to check to see if the hit is the player:
script.Parent.Touched:connect(function(hit)
local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
if player then
local PlrGui = player.PlayerGui
PlrGui.Sound:Destroy()
end
end)
local Player = game.Players.LocalPlayer
script.Parent.Touched:connect(function(hit)
local PlrGui = Player:WaitForChild("PlayerGui")
local Sound = PlrGui:FindFirstChild("Sound")
if not Sound then return end
Sound:Destroy()
end)
Have you tried putting this script (local script) in the StarterPlayerScripts, and not in workspace with the part.
I don’t think local scripts work in workspace. Change this line in the local script: script.Parent.Touched to the path of the part if you want to use a local script.
If you want to keep it in a server script and the same location, try this:
script.Parent.Touched:connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
local PlrGui = game.Players:GetPlayerFromCharacter(hit.Parent).PlayerGui
PlrGui:FindFirstChild("Sound")
PlrGui.Sound:Destroy()
end
end)
Here is some info on why you got the Errors you had:
Summary
The local Player doesn’t exist on the server and so, doesn’t exist on server scripts (or the normal scripts).
Local Scripts do not fire when they are in workspace, so parenting it to any part in workpace will never work