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

Need help with dialogue npc system! ?

Asked by
OX_X4 2
4 years ago

Explanation
So, I have a game with two npcs and when you come to them within a 6 stud radius a billboard gui appears with the keycap H and when you press H it starts up the dialogue. It works greatly but once i talk to anyone of them then go to the other the text glitches and the name doesn't change. I checked the scripts and it should work normally.

Scripts(3):

dialogue script

local player = game.Players.LocalPlayer
local CreateDialogueEvent = game.ReplicatedStorage.CreateDialogueEvent
local DialogueFrame = player.PlayerGui.dialogue.Frame
local order
--{0.278, 0},{0.656, 0}
local function playSound(sound_id) -- Plays typing sound
    local sound = Instance.new("Sound",game.ReplicatedStorage)
    sound.SoundId = sound_id
    sound.Volume = .1
    sound.PlayOnRemove = true
    sound:Destroy()
end

local function textAnimate(content) -- Animates each letter
    for i=1,string.len(content) do
        DialogueFrame.Content.Text = string.sub(content,1,i)
        playSound("rbxassetid://915576050")
        if string.sub(content,i,i) == "!" or string.sub(content,i,i) == "." or string.sub(content,i,i) == "?" then
            wait(1)
        elseif  string.sub(content,i,i) == "," then
            wait(.5)
        else
            wait(.05)
        end
    end
    content = nil
    return true
end

CreateDialogueEvent.Event:Connect(function(nameS, content, content2) -- activates when called from the Server
    if not player:findFirstChild("secretEnding") then
        DialogueFrame.name.Text = nameS
        DialogueFrame.Content.Text = ""
        DialogueFrame.Visible = true
        DialogueFrame.Continue.Visible = false
        local bool = textAnimate(content)
        print(bool)
        if bool == true and content2 ~= nil then
            wait(1)
            bool = textAnimate(content2)
            if bool == true then
                DialogueFrame.Continue.Visible = true
                DialogueFrame.Continue.MouseButton1Click:Connect(function()
                    DialogueFrame.Visible = false
                    player.Character.Humanoid.WalkSpeed = 16
                    nameS, content, content2 = nil
                end)
            end
        else
            DialogueFrame.Continue.Visible = true
            DialogueFrame.Continue.MouseButton1Click:Connect(function()
                DialogueFrame.Visible = false
                player.Character.Humanoid.WalkSpeed = 16
                nameS, content, content2 = nil
            end)
        end
    end
end)

npc 1 script

local inputServ = game:GetService("UserInputService")

local player = game.Players.LocalPlayer
local char = player.Character
local npc = game.Workspace.Bartender
local near = false
local clicked = false

local npcName = "Joe"
local content = npc.D1
local content2 = npc.D2 -- can be nill

local pTorso = char:FindFirstChild("UpperTorso")
local nTorso = npc.UpperTorso

while wait(0.3) do

    if (pTorso.CFrame.p - nTorso.CFrame.p).magnitude <= 6.25 and near == false and clicked == false then
        near = true
        npc.PresH.Enabled = true
        -- check for H input
        inputServ.InputBegan:Connect(function(input, gameProccesed)
            if input.UserInputType == Enum.UserInputType.Keyboard and clicked == false then
                if input.KeyCode == Enum.KeyCode.H and clicked == false then
                    clicked = true
                    npc.PresH.Enabled = false
                    game.ReplicatedStorage.CreateDialogueEvent:Fire(npcName, content.Value, content2.Value)
                end
            end
        end)
    elseif (pTorso.CFrame.p - nTorso.CFrame.p).magnitude > 6.25 then
        near = false
        npc.PresH.Enabled = false
        clicked = false
    end
end

npc 2 script

local inputServ = game:GetService("UserInputService")

local player = game.Players.LocalPlayer
local char = player.Character
local npc = game.Workspace.DrDenaro
local near = false
local clicked = false

local npcName = "Dr. Denaro"
local content = npc.D1
local content2 = nil -- can be nill

local pTorso = char:FindFirstChild("UpperTorso")
local nTorso = npc.UpperTorso

while wait(0.3) do

    if (pTorso.CFrame.p - nTorso.CFrame.p).magnitude <= 6.25 and near == false and clicked == false then
        near = true
        npc.PresH.Enabled = true
        -- check for H input
        inputServ.InputBegan:Connect(function(input, gameProccesed)
            if input.UserInputType == Enum.UserInputType.Keyboard and clicked == false then
                if input.KeyCode == Enum.KeyCode.H and clicked == false then
                    clicked = true
                    npc.PresH.Enabled = false
                    game.ReplicatedStorage.CreateDialogueEvent:Fire(npcName, content.Value, content2)
                end
            end
        end)
    elseif (pTorso.CFrame.p - nTorso.CFrame.p).magnitude > 6.25 then
        near = false
        npc.PresH.Enabled = false
        clicked = false
    end
end

Answer this question