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

hit.Parent not working, took 8 month break, and now my code won't work, what is wrong?

Asked by
Mpire_z -2
5 years ago

Hello, My name is Mpire, and I tried to make this script for a shop gui. local gui = script.Parent.ShopGui wait(1) script.Parent.Touched:Connect(function(hit) local PlrName = hit.Parent.Name local Plr = game.Players.PlrName local clone = gui:Clone() clone.Name = "ShopClone" clone.Parent = Plr.Playergui

end) I am somewhat advanced at scripting, and i've been doing this for a long time, and i took a 8 month break from roblox, and now all of a sudden, this doesn't work... And here's the error " 13:11:30.584 - PlrName is not a valid member of Players "

0
AdD a CoDe BlOcK Mr_Unlucky 1085 — 5y

6 answers

Log in to vote
0
Answered by
Mpire_z -2
5 years ago
    local gui = script.Parent.ShopGui
wait(1)
script.Parent.Touched:Connect(function(hit)
    local PlrName = hit.Parent.Name
    local Plr = game.Players.PlrName
    local clone = gui:Clone()
    clone.Name = "ShopClone"
    clone.Parent = Plr.Playergui


end)
0
Sorry about that, forgot to add code block... Mpire_z -2 — 5y
0
try game.Players.LocalPlayer.PlrName IcyMizu 122 — 5y
1
You can edit your original post. xPolarium 1388 — 5y
Ad
Log in to vote
0
Answered by
HaveASip 494 Moderation Voter
5 years ago
Edited 5 years ago

All the other answers are bad, and there may be many mistakes.

Try this script:

local Gui = script.Parent:WaitForChild("ShopGui")

script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then --Checks if hit parent is player(not part or brick)
        local Char = hit.Parent
        local Player = game:GetService("Players"):GetPlayerFromCharacter(Char)
        local ClonedGui = Gui:Clone()
        ClonedGui.Parent = Player:WaitForChild("PlayerGui")
        ClonedGui.Name = "Cloned" --Your cloned gui name here
    end
end)

So here is your script. I will explain your errors.

local Gui = script.Parent.ShopGui
wait(1)
script.Parent.Touched:Connect(function(hit)
    local PlrName = hit.Parent.Name
    local Plr = game.Players.PlrName
    local clone = gui:Clone()
    clone.Name = "ShopClone"
    clone.Parent = Plr.Playergui
end)
  1. In your code second line useless, so you can remove that
  2. I recommend you to use if hit.Parent:FindFirstChild("Humanoid"). That will check if the brick hited by player
  3. If you have variable with player's name and you want to get the player from Players service you need to use game.Players:FindFirstChild(PlrName). If you use game.Players.PlrName game will think that you want to get the player with "PlrName" name.

P.s I am sorry for my bad english.

0
Please explain your code in your answer. Otherwise, OP won't learn anything. lunatic5 409 — 5y
0
i only fixed his error nothing else IcyMizu 122 — 5y
0
@ Darkwolfgameryr you didnt fixed that. What's means game.Players.LocalPlayer.PlrName? Really? HaveASip 494 — 5y
0
@lunatic5 O'key. HaveASip 494 — 5y
0
oh right misread the error IcyMizu 122 — 5y
Log in to vote
0
Answered by 5 years ago

You can't use .PlrName when looking for a specific player, in fact, you can't use any variables after a dot. To reference a variable, you must use square brackets. That would make your script look like this:

local gui = script.Parent.ShopGui
-- took out the wait, as it wasn't necessary
script.Parent.Touched:Connect(function(hit)
    local PlrName = hit.Parent.Name
    local Plr = game.Players[PlrName]
    local clone = gui:Clone()
    clone.Name = "ShopClone"
    clone.Parent = Plr.Playergui
end)

Another useful thing that square brackets can do is reference models with spaces in their name. This can be very helpful and I suggest you keep it in mind. An example of this would be if you were making a tycoon and added a coal dropper. If you wanted to name it coal dropper with a space to make it look neater in your eyes, you could, it would just require a different code.

Hope I helped. -Cmgtotalyawesome

Log in to vote
0
Answered by 5 years ago

I think the reason for the error is that studio is now subjected to Filtering Enabled due to the accurate studio update and that you are trying to look for a string that is a child of game.Players rather than a player object. You could have done this to avoid the error by doing:

local plrName = hit.Parent.Name
local plr = game.Players[plrName]

--or use find first child

local plrName = hit.Parent.Name
local plr = game.Players:FindFirstChild(plrName)

Correct me if I'm wrong but I think you are using a server script, which is fine, but you can easily achieve the same goals by using a local script.

--I placed this local script inside the shopGUI
local plr = game.Players.LocalPlayer
workspace.ShopPart.Touched:Connect(function(hit)
    if hit and hit:IsDescendentOf(plr.Character) then
        local shopFrame = script.Parent.Frame
        shopFrame.Visible = true 
    end
end)

you can have the gui inside the starter gui and there really isnt any need for cloning as long as you have one frame that encompasses every other GUI element of the shop gui

Log in to vote
-2
Answered by
Hizar7 102
5 years ago

Hi, is the gui an actual GUI ? if so i Would say you need to do a clicked event, like MouseButton1Down:Connect

touched event is if the actual characters body part was to touch it, or another part for that matter. Let me know! I've never known to use a touched event for a gui

Log in to vote
-3
Answered by
IcyMizu 122
5 years ago

you gotta do this bcz u diddnt add LocalPlayer

 local gui = script.Parent.ShopGui
    wait(1)
    script.Parent.Touched:Connect(function(hit)
        local PlrName = hit.Parent.Name
        local Plr = game.Players.LocalPlayer.PlrName
        local clone = gui:Clone()
        clone.Name = "ShopClone"
        clone.Parent = Plr.Playergui
            end)

if it still wrong dm me

Answer this question