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

Script Shows GUI on the first Chest, but then doesnt show gui on any other chests, why?

Asked by
LuaDLL 253 Moderation Voter
5 years ago

My Problem is that when i spawn in, it spawns the chests, then I go up to the first one and it shows the gui. But when I walk Up To The Second Chest it does not show the gui for some reason but i can go back to the first chest and it will show the gui again.

First Chest: https://gyazo.com/ff7057c2c19c30ec3d8df1f56ee9038a

Second Chest: https://gyazo.com/6d4603721e8f8143cd7eede6d7bd78e1

Explorer Images: Chest Location Gui Location

Script:

local Chests = workspace.Chests
local Range = 10
local P = game.Players.LocalPlayer
local C = P.Character or P.CharacterAdded:Wait()
local Hb = game:GetService("RunService").Heartbeat

function Check()
    for i,v in pairs(Chests:GetChildren()) do
        if (v.Hitbox.Position - C.PrimaryPart.Position).Magnitude <= Range and not v.Openers:FindFirstChild(P.Name) then
            local Pos = workspace.CurrentCamera:WorldToScreenPoint(v.Hitbox.Position)
            script.Parent.Open.Position = UDim2.new(0,Pos.X,0,Pos.Y,0)
            script.Parent.Open.Visible = true
        else
            if script.Parent.Open.Visible then
                script.Parent.Open.Visible = false
            end
        end
    end
end

workspace.Chests.ChildAdded:Connect(Check)
Hb:Connect(Check)

1 answer

Log in to vote
0
Answered by 5 years ago

Use ContextActionService to bind an action to the 'E' key when the player gets close to the chest, then use ContextActionService: SetImage to have the GUI appear (https://developer.roblox.com/en-us/api-reference/function/ContextActionService/SetImage), then when the player is too far from the chest, unbind the action from the key and rebind it again when the player is close enough to a chest. Just use remote events when the player presses the 'E' from a local script and then fire the server. I hope it helps!

Ad

Answer this question