I have a car shop GUI I have made and I want it to open up by stepping on a part. I have a part down and a local script. The local script has
script.Parent.Touched:connect(function() local player = game.Players.LocalPlayer player.PlayerGui.Carshop.Frame.Visible = true end)
Technically, I'm not allowed to post scripts, but I'm going to do it anyway, and explain what each part does.
--make sure that this script is directly in the part script.Parent.Touched:connect(function(part) --touched event local player = game.Players:FindFirstChild(part.Parent.Name) --if the player steps on it, the player will exist. if, say, a random block touches it, "player" will be nil if player then --makes sure player exists player.PlayerGui:FindFirstChild("CarShop"):FindFirstChild("Frame").Visible = true --makes the GUI visible end end)
And there you have it. If you have any questions, comment. Please accept the answer and upvote if the answer is useful!
The script doesn't know who the player is, so you need an argument for that
function Touch(hit) --hit is your argument, hit is the player that touched the block game.Players:FindFirstChild(hit.Parent.Name).PlayerGui.Carshop.Frame.Visible = true end script.Parent.Touched:connect(Touch)
LocalScripts must be a child of a child of parent. Or in a character. So we need to put this in a REGULAR SCRIPT.
script.Parent.Touched:connect(function(character) if game.Players:GetPlayerFromCharacter(character.Parent) then local player = game.Players:GetPlayerFromCharacter(character.Parent) player.PlayerGui.Carshop.Frame.Visible = true end)
We also used the :GetPlayerFromCharacter()
function. In the parameter you need the add the character. Regular scripts can't use LocalPlayer.
You didn't have anything in the parameter because it was a local script. Now we use the parameter here to check if it's a player(parameter was character). We also use the parameter to find the character.
I Hope this Helps!