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

Need help with this GUI Magnitude script?

Asked by
Xl5Xl5 65
8 years ago

I need help with this script, basically what this script does is when I approach an NPC speficially named "Bob" and I am 5 studs away from him on keypress the dialog gui pops up this all works absolutely fine. But I would like to know how to add more NPCs to this without spamming this,

 if (game.Players.LocalPlayer.Character.Torso.Position - Workspace.Bob.Torso.Position).magnitude <= distance then

elseif (game.Players.LocalPlayer.Character.Torso.Position - Workspace.Jim.Torso.Position).magnitude <= distance then

but instead I wish to put in a table to affect the listed npcs. NPC = {"Bob","Jim"} and also use the table, to find which NPC dialog to open. JimChat GUI or BobChat GUI

For example When the player is 5 or less studs away from Bob, I press E Bobs chat will appear.

My hierarchy. StarterGUI> -LocalScript

Workspace> -NPCs <-- with multiple children with different names i.e. Bob and Jim. --Bob --Jim

ReplicatedStorage> -BobChat -JimChat

How would I go about doing this? script below. Sorry if this really confusing, please help! much appreicated!

mouse = game.Players.LocalPlayer:GetMouse()
distance = 5 -- stud radius from object
Player = script.Parent.Parent

mouse.KeyDown:connect(function(key)
    local box = "BobChat"
    local gui = Player:WaitForChild("PlayerGui"):FindFirstChild(box)

                      if (game.Players.LocalPlayer.Character.Torso.Position - Workspace.Bob.Torso.Position).magnitude <= distance then
                lol = "e"
                       if (key:lower()) == lol then   
                       if Player:WaitForChild("PlayerGui"):FindFirstChild(box) then
Player:WaitForChild("PlayerGui"):FindFirstChild(box):Destroy()
                        else
game.ReplicatedStorage:FindFirstChild(box):Clone().Parent = Player.PlayerGui


                                    end
                            end
                    end

end)

1 answer

Log in to vote
2
Answered by
BlackJPI 2658 Snack Break Moderation Voter Community Moderator
8 years ago

Your suggestion of storing NPCs in a table will work great. A nice way to do this is with a dictionary with the indices being the NPCs and the stored value being the chat GUI associated with that character. In my example, I make the value an array storing the distance at which you can chat from and that particular NPC's chat GUI. You could add or remove more variables to this list.

local NPCChat = {
    [game.Workspace.Bob] = {
        distance = 5, 
        GUI = game.ReplicatedStorage.BobChat
    },
    [game.Workspace.Jim] = {
        distance = 5, 
        GUI =game.ReplicatedStorage.JimChat
    }
}

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

mouse.KeyDown:connect(function(key)
    if key == "e" then
        for NPC, properties in pairs(NPCChat) do
            if player.Character and (player.Character.Torso.Position - NPC.Torso.Position).magnitude <  properties.distance and not player.PlayerGui:FindFirstChild(properties.GUI.Name) then
                properties.GUI:Clone().Parent = player.PlayerGUI
            end
        end
    end
end
Sorry for the long if statement
0
I am getting "21:22:53.570 - Players.Player1.PlayerGui.KeyDialog:6: '}' expected (to close '{' at line 1) near '['" what does that mean? Xl5Xl5 65 — 8y
0
I forgot a comma on line 5, should be better now BlackJPI 2658 — 8y
0
Don't worry I fixed it, added a comma after the } on line 5, and FindFirstChild("PlayerGui") and end). Many thanks for your help! much appreciated ^^ Xl5Xl5 65 — 8y
0
No problem! BlackJPI 2658 — 8y
Ad

Answer this question