onTouched Event pop up Gui dosen't work

Hey I made script with Event Touched, and I want when someone touch it with body it pop up Gui from StarterGui. But when I touch it it dosen’t work. I used Script (not localscript) and insert it in part.
Here is script:

local plr = game.Players.LocalPlayer
local Player = game:GetService("Players") -- Get LocalPlayer
local leaderstats = Player:WaitForChild("leaderstats") -- Get leaderstats
local teleport = script.Parent.Parent.Teleport -- Get Teleport
local buyed = plr.Islands.island1 -- Get BoolValue from FirstIsland is Buyed
local buyislandgui = plr.PlayerGui.BuyIslandGUI.IslandFrame -- Get IslandFrame from StarterGui

local cost = 500 -- Cost
local cost2 = 20 -- Cost2
local elemental = leaderstats.Dirt -- Dirt, Grass, Rock
local elemental2 = leaderstats.Grass -- Dirt, Grass, Rock

-- Function onTouched(hit)
script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        buyislandgui.Visible = true
    end
end)
1 Like

LocalPlayer is nil for Scripts.

You should be using a LocalScript

2 Likes

You would need to use a Local Script. Local Scripts are used to alter things on the client side. Such as a player’s backpack, character, or GUI.

still dosen’t works :((
And no error pops up

Why do you have this? You should have wrote:

local Players = game:GetService("Players")
local plr = Players.LocalPlayer

don’t use localplayer in a script

also,

script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        buyislandgui.Visible = true
    end
end)

doesn’t work because there is no plr

if you really want to make the GUI visible server sided you would need to do it like

function getPlayerFromString(name)
 for i,v in pairs(game.Players:GetPlayers()) do
  if v.Name == name then
   return v
  end
 end
return false
end

script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        local x = getPlayerFromString(hit.Parent.Name)
        if x == false then error("Something went wrong.") else
         local pgui = x.PlayerGui
         local gui = pgui.BuyIslandGui.IslandFrame
        gui.Visible = true
        end
    end
end)