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

How do I find playergui in a script (to get to my gui within there), or what should I do?

Asked by 4 years ago
Edited 4 years ago
local part = game.workspace.BattleSigil1.PartSigil1friendly
local part1 = game.workspace.BattleSigil1.PartSigil1enemy
function onTouched(hit)
    print("hit")
    local character = hit.Parent
    if character and character:findFirstChild("Humanoid") then
        local b = Instance.new("BodyPosition")
        b.position = Vector3.new(part.Position.X,part.Position.Y +0.5,part.Position.Z)
        b.maxForce = Vector3.new(50000, 50000, 50000)
        b.Parent = character.UpperTorso
    local character1 = script.Parent
    local b1 = Instance.new("BodyPosition")
        b1.position = Vector3.new(part1.Position.X,part1.Position.Y +0.5,part1.Position.Z)
        b1.maxForce = Vector3.new(50000, 50000, 50000)
        b1.Parent = character1
        print("init battle")
        local rem = game.ReplicatedStorage.RemoteEvent

end
    end
script.Parent.Touched:connect(onTouched)

I tried figuring out how to go to the playergui, because im trying to show a gui in there and I cant figure it out. I tried doing stuff like game.Players.LocalPlayer.PlayerGui

and even tried using remote events, but idk how that'd work

local Playergui = game.Players.LocalPlayer:WaitForChild("PlayerGui")
local Spells = Playergui.BattleSigilSpells.Frame
local rem = game.ReplicatedStorage.RemoteEvent

thats what I tried.

Also might be important this is obviously a thing within workspace, its a model. and the model is a humanoid, this script is in the torso.

1 answer

Log in to vote
1
Answered by
DanzLua 2879 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

We aren't limited to using a remoteevent for this since the server can see inside a player's playergui.

The reason your script won't work is because you are attempting to use localplayer which can only be used on a localscript. To fix this we need to get the character's player instance and use that for finding the playergui. We can use the GetPlayerFromCharacter() function of player

so

local part = game.workspace.BattleSigil1.PartSigil1friendly
local part1 = game.workspace.BattleSigil1.PartSigil1enemy
function onTouched(hit)
    print("hit")
    local character = hit.Parent
    if character and character:findFirstChild("Humanoid") then
        local b = Instance.new("BodyPosition")
        b.position = Vector3.new(part.Position.X,part.Position.Y +0.5,part.Position.Z)
        b.maxForce = Vector3.new(50000, 50000, 50000)
        b.Parent = character.UpperTorso
    local character1 = script.Parent
    local b1 = Instance.new("BodyPosition")
        b1.position = Vector3.new(part1.Position.X,part1.Position.Y +0.5,part1.Position.Z)
        b1.maxForce = Vector3.new(50000, 50000, 50000)
        b1.Parent = character1
        print("init battle")

    --doing stuff to playergui
    --get player
    local plr=game.Players:GetPlayerFromCharacter(character)
    --find playergui
    local Playergui = plr:WaitForChild("PlayerGui")
    local Spells = Playergui.BattleSigilSpells.Frame


end
    end
script.Parent.Touched:connect(onTouched)

SECOND WAY yes you could use a remoteevent, but how?

First things first, in your server script we need to set the remoteevent variable, specifically at the begginging of the script, then when the part is touched, we fire to the client of the player who touched it. So after a character touches the part, we need to get its player instance and send that through the remoteevent. We can use the GetPlayerFromCharacter() function of player

ServerScript

--first
local rem = game.ReplicatedStorage.RemoteEvent

local part = game.workspace.BattleSigil1.PartSigil1friendly
local part1 = game.workspace.BattleSigil1.PartSigil1enemy
function onTouched(hit)
    print("hit")
    local character = hit.Parent
    if character and character:findFirstChild("Humanoid") then
        local b = Instance.new("BodyPosition")
        b.position = Vector3.new(part.Position.X,part.Position.Y +0.5,part.Position.Z)
        b.maxForce = Vector3.new(50000, 50000, 50000)
        b.Parent = character.UpperTorso
    local character1 = script.Parent
    local b1 = Instance.new("BodyPosition")
        b1.position = Vector3.new(part1.Position.X,part1.Position.Y +0.5,part1.Position.Z)
        b1.maxForce = Vector3.new(50000, 50000, 50000)
        b1.Parent = character1
        print("init battle")
    --get player
    local plr=game.Players:GetPlayerFromCharacter(character)
        rem:FireClient(plr)

end
    end
script.Parent.Touched:connect(onTouched)

Now on the client we need to set a event function for when that remoteevent is triggered. When there is an event we will run the code needed Make sure it is a localscript located somewhere like starterGui.

LocalScript

--set rem variable
local rem = game.ReplicatedStorage.RemoteEvent

--setting event function
rem.OnClientEvent:Connect(function()
    local Playergui = script.Parent --If script is located in startergui then leave this alone.
    local Spells = Playergui.BattleSigilSpells.Frame
    local rem = game.ReplicatedStorage.RemoteEvent
end)

And tada! Let me know if you have questions.

Ad

Answer this question