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
1 | script.Parent.Touched:connect( function () |
2 | local player = game.Players.LocalPlayer |
3 | player.PlayerGui.Carshop.Frame.Visible = true |
4 | end ) |
Technically, I'm not allowed to post scripts, but I'm going to do it anyway, and explain what each part does.
1 | --make sure that this script is directly in the part |
2 |
3 | script.Parent.Touched:connect( function (part) --touched event |
4 | 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 |
5 | if player then --makes sure player exists |
6 | player.PlayerGui:FindFirstChild( "CarShop" ):FindFirstChild( "Frame" ).Visible = true --makes the GUI visible |
7 | end |
8 | 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
1 | function Touch(hit) --hit is your argument, hit is the player that touched the block |
2 | game.Players:FindFirstChild(hit.Parent.Name).PlayerGui.Carshop.Frame.Visible = true |
3 | end |
4 |
5 | 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.
1 | script.Parent.Touched:connect( function (character) |
2 | if game.Players:GetPlayerFromCharacter(character.Parent) then |
3 | local player = game.Players:GetPlayerFromCharacter(character.Parent) |
4 | player.PlayerGui.Carshop.Frame.Visible = true |
5 | 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!