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

Does anyone know why this keeps cloning the screengui? and cloning even if ur not within 5 studs?

Asked by 5 years ago
Edited 5 years ago

so whenever a player types and is 5 studs within the part then it should like show the message that he has typed and also this clones the screengui even if u are not 5 studs away it still clones it

local mic = workspace.AnnouncerTable.MIC.Main
local range = 5

game.Players.PlayerAdded:Connect(function(p)
    p.CharacterAdded:Connect(function(char)
        p.Chatted:Connect(function(msg)
        while true do
        if char ~= nil then
            local torso = char.Torso
            if (mic.Position-torso.Position).magnitude <= range then
    local screengui = Instance.new("ScreenGui",p.PlayerGui)
    screengui.Name = "Announcement"
    local textlabel = Instance.new("TextLabel",p.PlayerGui:FindFirstChild("Announcement"))
    textlabel.Text = msg:lower()
    textlabel.Size = UDim2.new(1,0,1,0)
    textlabel.TextScaled = true
    textlabel.BackgroundTransparency = .8
                print(p.Name.." Is close") 
                wait(1)
                screengui:Destroy()
                break
                end
            end
        end)
    end)
end)

1 answer

Log in to vote
1
Answered by
xPolarium 1388 Moderation Voter
5 years ago
Edited 5 years ago

Using a while loop in chatted even when you don't need to.

local mic = workspace.Mic --some part in workspace
local range = 5 --in studs btw

game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(char)
        player.Chatted:Connect(function(msg)

--You should only have a while loop when you want to run the same code
--You're only doing this once every Chatted event so just lose the while

            --Check to see if player isn't dead
            if char.Humanoid.Health > 0 then

--Every R6 or R15 character has a HumanoidRootPart. Better than using torso.
                local hrp = char:WaitForChild("HumanoidRootPart")

                --Check distance
                if (mic.Position-hrp.Position).magnitude <= range then
                    local screengui = Instance.new("ScreenGui")
                    screengui.Name = "Announcement"
                     --You don't even need to rename this

                    screengui.Parent = player.PlayerGui 
                    --because you defined the new instance

                    local textlabel = Instance.new("TextLabel")
                    textlabel.Text = msg:lower()
                    textlabel.Size = UDim2.new(1,0,1,0)
                    textlabel.TextScaled = true
                    textlabel.BackgroundTransparency = .8
                    textlabel.Parent = screengui

                    print(player.Name.." is close") 
                    --debug print could be at the start of the if-statement

                    game:GetService("Debris"):AddItem(screengui, 1) 
                    --Same thing as before
                end
            end
        end)
    end)
end)

edit: fix format

The one thing this doesn't do is let other player's see it. You can just use a for loop to create one for each player. Should be easy to do for you but let me know if you need help on that.

Let me know if you need more explanation.

0
one more thing how do u make the cooldown thing longer WillBe_Stoped 71 — 5y
0
For the gui to disappear? Change the 1 on line 36. That's the wait time until it's cleared. xPolarium 1388 — 5y
0
ok thanks WillBe_Stoped 71 — 5y
Ad

Answer this question