Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

Pls help ShopGui Enabling Script doesnt Work. Its Touch Based.?

Asked by 3 months ago
local detector = script.Parent

function opengui()
    local shopgui = game.Players.LocalPlayer.PlayerGui.ShopGui
    shopgui.Enabled = true
end

detector.Touched:Connect(opengui)

pls help this error is too important 4 me :))

1 answer

Log in to vote
0
Answered by 3 months ago
Edited 3 months ago

I'm assuming this is a local script, correct? If so, local scripts do not work inside workspace. Local Scripts will only run if it is a descendant of one of the following objects: A player's Backpack, Character Model, PlayerGui, PlayerScripts and also ReplicatedFirst

This can be fixed by changing the location of your local script from workspace to StarterGui (or anywhere that was stated above).

Edit: Since you stated it was not a local script, game.Players.LocalPlayer won't work inside a server script so I'll provide a script that will work inside a Server Script.

So a touched event has a parameter that returns the other part that came in contact with the given part. We can then use this parameter to check if the parent of it has a child called 'Humanoid' so we know if it's a character. Note that this will also detect NPCs.

After we know that it's a character, we can then get the player from the character by using the Players:GetPlayerFromCharacter function.

Then all you'll need to do is finally get the GUI from the Player's PlayerGui.

script.Parent.Touched:Connect(function(hit)

    if hit.Parent:FindFirstChild("Humanoid") then
        local Character = hit.Parent
        local Player = game.Players:GetPlayerFromCharacter(Character) -- Getting the player from the player's Character
        local ShopGui = Player.PlayerGui.ShopGui
        ShopGui.Enabled = true
    end
end)

Also if you do have NPCs in your game and they may walk over this part, you may want to use game.Players:GetPlayerFromCharacter(hit.Parent) on the if statement instead of using FindFirstChild to find a Humanoid, this way we know if it's actually a player that is touching the part. Reason why you'd want to do this is if an NPC walks over the part it's going to error because you won't be able to get the Player from that NPC because it doesn't have one.

Hope this helps.

Please do let me know if it does not work.

0
its not a local script Omerevkizel 16 — 3mo
0
Alright, I assumed it was a local script since you were using `Players.LocalPlayer`, which only works in local scripts. I'll edit my answer. xInfinityBear 1026 — 3mo
0
oh thank you Omerevkizel 16 — 3mo
0
um i have a question Omerevkizel 16 — 3mo
Ad

Answer this question