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

How can i make a player get a gui i have in a part if they touch it?

Asked by 6 years ago
Edited 6 years ago
script.Parent.Touched:connect(function()
    script.Parent.shop:Clone().Parent = game.Players.LocalPlayer.PlayerGui
    print("done")
end)

that is the not working code please help me !

0
I want to make it so if the player touches the part it gives the player the shop gui and opens it DanielDeJong -13 — 6y

3 answers

Log in to vote
1
Answered by
Azarth 3141 Moderation Voter Community Moderator
6 years ago
Edited 6 years ago

You're using a Server script, which can't access the LocalPlayer. A Server script has no idea who the client is. Every location (Starterpack, PlayerGui, StarterCharacterScripts, StarterPlayerScripts, etc..) that a LocalScript can be placed becomes in some way a descendant or child of a user when your player loads. However, place a LocalScript in workspace and that script is no longer associated with any player, because it's a child of workspace. This, however, doesn't mean you can't get the player from the part you touched.

This goes in a normal/server script.

local part = script.Parent
-- Use WaitForChild() on children
    -- Scripts load before objects online
local shop = part:WaitForChild("shop")
-- Use 'C'onnect
part.Touched:Connect(function(hit)
    -- Make sure hit didn't suddenly get destroyed
    -- Hit is what hit the brick, like your leg. 
    if hit and hit.Parent then 
        local player = game.Players:GetPlayerFromCharacter(hit.Parent)
        if player then
            local char = player.Character
            local humanoid = char:findFirstChild("Humanoid")
            local player_gui = player:findFirstChild("PlayerGui")
            -- Make sure we didn't reset on it and we don't already
                -- have the shop
            if humanoid and humanoid.Health > 0 and not player_gui:findFirstChild("shop") then 
                local shop_clone = shop:Clone()
                shop_clone.Parent = player_gui
            end
        end
    end
end)
Ad
Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

This goes in a normal script inside of the part. You need to use GetPlayerFromCharacter, that way you can clone the GUI into their PlayerGui.

script.Parent.Touched:Connect(function(hit)
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
local gui = script.Parent.ScreenGui:Clone()
gui.Parent = player.PlayerGui
end)
--Edit: Made a typo. My bad

If this helped, please upvote and accept answer! Thanks!

0
None of all the answers worked?! DanielDeJong -13 — 6y
0
What is your hierarchy? PyccknnXakep 1225 — 6y
0
Wut is that DanielDeJong -13 — 6y
0
Where is your script located and where is your Gui located. PyccknnXakep 1225 — 6y
View all comments (29 more)
0
Inside the part DanielDeJong -13 — 6y
0
Where is the ScreenGui located PyccknnXakep 1225 — 6y
0
also inside the part DanielDeJong -13 — 6y
0
its all located inside the part i want to be touched DanielDeJong -13 — 6y
0
Did you edit my answer at all? You might need to change line three to local gui = script.Parent.shop:Clone() , since your shop gui is named "shop" and not "Screengui". "ScreenGui" was an example. PyccknnXakep 1225 — 6y
0
i named the gui ScreenGui DanielDeJong -13 — 6y
0
Take a snapshot on where everything is located because I tested this script myself and It worked perfectly fine. PyccknnXakep 1225 — 6y
0
What is inside of the script? You must have made a typo or something. PyccknnXakep 1225 — 6y
0
script.Parent.Touched:Connect(function(hit) local player = game.Players:GetPlayerFromCharacter(hit.Parent) local gui = script.Parent.ScreenGui:Clone() gui.Parent = player.PlayerGui end) DanielDeJong -13 — 6y
0
You probably have the outdated version of my answer in the script. Delete the contents and copy and paste my updated answer above. I made an accidental typo when I first submitted my answer. I apologize. PyccknnXakep 1225 — 6y
0
I already did that. ill do it again DanielDeJong -13 — 6y
0
I literally just tested it and it's working perfectly fine for me. PyccknnXakep 1225 — 6y
0
Not for me DanielDeJong -13 — 6y
0
Check the output and see if theres any errors. PyccknnXakep 1225 — 6y
0
No errors, It just won't work.. here is proofThat it should work http://prntscr.com/g71bzz DanielDeJong -13 — 6y
0
Well my answer definitely works. It might be because of your character or something. Did you use any custom character scripts? PyccknnXakep 1225 — 6y
0
Nope.. http://prntscr.com/g71cw8 im in R15 tho i don't think but maybe it has something to do with this? http://prntscr.com/g71d30 DanielDeJong -13 — 6y
0
You have CanCollide turned off. You need to turn it on for it to work. PyccknnXakep 1225 — 6y
0
There is something wrong, if i click play the block just disappears DanielDeJong -13 — 6y
0
Is it Anchored? PyccknnXakep 1225 — 6y
0
yep DanielDeJong -13 — 6y
0
I don't know then. My script works 100%. Something in your game is screwing it up. Sorry. PyccknnXakep 1225 — 6y
0
Can you make it a model and send me the link?. DanielDeJong -13 — 6y
0
I guess PyccknnXakep 1225 — 6y
0
works.. DanielDeJong -13 — 6y
0
Im so dumb the gui i had wasn't enabled?!?!?! Grrrr DanielDeJong -13 — 6y
0
Lol. Please accept my answer as I went through all this trouble to have it work for you, PyccknnXakep 1225 — 6y
Log in to vote
-1
Answered by 6 years ago
Edited 6 years ago

--Sorry I forgot the Humanoid part!

Instructions

1. Place the GUI in the part.

2. place the script in the part

local gui = script.Parent.GUINAME

script.Parent.Touched:Connect(function(hit)
    if hit.Parent.Name == 'Humanoid' then
        gui:Clone().Parent = game.Players.LocalPlayer.PlayerGui
    elseif hit.Parent.Name ~= nil then
        gui:Remove().Parent = game.Players.LocalPlayer.PlayerGui
    end
end)
0
That's incorrect. You need to use GetPlayerFromCharacter. You cannot index LocalPlayer from a normal script in the workspace. PyccknnXakep 1225 — 6y
0
Or you atleast need to get the player who touched the part. PyccknnXakep 1225 — 6y
0
Are you trying to make mine sound like it doesn't work so you could get upvoted?! Goldenkings11 -11 — 6y
0
now I made it where if it finds the humanoid that touched the part then it opens it for the localplayer. Goldenkings11 -11 — 6y
View all comments (2 more)
0
That is not the reason. The reason was is because your answer wouldn't work if I beginner tried to use it. Again, LocalPlayer can only be indexed from a LocalScript. Not a server script. PyccknnXakep 1225 — 6y
0
*A beginner PyccknnXakep 1225 — 6y

Answer this question