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

Why is this GUI /sound script not working when I touch this brick?

So I made a brick that when you touch it, a picture is supposed to pop up and play a sound...

script.parent.Touched:connect(function()
	local s = Instance.new("ScreenGui",game.Players.LocalPlayer.PlayerGui)
	local i = Instance.new("ImageLabel",game.Players.LocalPlayer.PlayerGui.ScreenGui)
	i.Image = "rbxgameasset://Images/fat fedora guy"
	i.Size = UDim2.new(0.559, 0,0.683, 0)
	i.Position =UDim2.new(0.211, 0,0.089, 0)
	local m = Instance.new("Sound",game.Workspace)
	m.Volume = 1
	m.SoundId = "rbxassetid://256126911"
	m:Play()
	wait(5)
	s:Destroy()
	i:Destroy()
	m:Destroy()
end)

Why isn't this script working? I tried putting it in a normal script, as well, and it still didn't work.

2 comments

1

You can only access local player in a local script

0

Also instead of doing "rbxassetid://256126911" do "http://www.roblox.com/asset/?id=256126911" for SoundId

1 answer

✓ Accepted answer

LocalPlayer is LOCAL SCRIPT ONLY! This means you can't use a Script if you want to link it to LocalPlayer.

However, there is another way to do this. We just need to find the player who touched the brick. If there is no such player, the function does nothing.

Here, let me show you the script.

script.parent.Touched:connect(function(hitObject) -- The touched event returns the object that it touches.
    local player = game.Players:FindFirstChild(hitObject.Parent.Name) -- The player is a model in Workspace. However, the hitObject only returns what PART of the player it touches, i.e. Left Leg, Torso, Right Arm.
    if player then
    	local s = Instance.new("ScreenGui", player:WaitForChild("PlayerGui"))
    	local i = Instance.new("ImageLabel", s) -- You have already made a reference to the "ScreenGui"
    	i.Image = "rbxgameasset://Images/fat fedora guy"
    	i.Size = UDim2.new(0.559, 0,0.683, 0)
    	i.Position =UDim2.new(0.211, 0,0.089, 0)
    	local m = Instance.new("Sound", game.Workspace)
    	m.Volume = 1
    	m.SoundId = "http://www.roblox.com/asset/?id=256126911" --Recommended!
    	m:Play()
   	 wait(5)
    	s:Destroy()
    	i:Destroy()
    	m:Destroy()
    end
end)

If you have any questions, please leave a comment down below. Thank you and I hope this will help you!

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