只需显示其中一条消息,但它同时显示了全部10条。

huangapple go评论61阅读模式
英文:

I need one message to be displayed out of 10. but it displays all 10 at the same time

问题

我正在使用Lua制作一个Roblox游戏。在游戏中,我想让玩家站在一个方块上,该方块会变成红色,并显示从10条消息中随机选择的一条消息。但是所有10条消息都被显示出来。

以下是您提供的代码的翻译部分:

local block = script.Parent
local originalColor = block.BrickColor
local messages = {
    "消息 1",
    "消息 2",
    "消息 3",
    "消息 4",
    "消息 5",
    "消息 6",
    "消息 7",
    "消息 8",
    "消息 9",
    "消息 10"
}

local function onTouched(part)
    local humanoid = part.Parent:FindFirstChild("Humanoid")
    if humanoid then
        block.BrickColor = BrickColor.new("Bright red")
        local text = Instance.new("BillboardGui", block)
        text.Adornee = block
        text.Size = UDim2.new(1, 0, 1, 0)
        text.StudsOffset = Vector3.new(0, 2, 0)
        local label = Instance.new("TextLabel", text)
        label.Size = UDim2.new(1, 0, 1, 0)
        label.Text = messages[math.random(#messages)]
        label.TextSize = label.TextSize + 10
        label.TextColor3 = Color3.new(1, 1, 1)
        label.BackgroundTransparency = 1
        wait(3)
        block.BrickColor = originalColor
        text:Destroy()
    end
end

block.Touched:Connect(onTouched)
英文:

I am making a roblox game using lua. in it i want a player to stand on a block which will turn red and display a randomly chosen message from 10 messages. But all 10 messages are being displayed.

local block = script.Parent
local originalColor = block.BrickColor
local messages = {
    "Message 1",
    "Message 2",
    "Message 3",
    "Message 4",
    "Message 5",
    "Message 6",
    "Message 7",
    "Message 8",
    "Message 9",
    "Message 10"
}

local function onTouched(part)
    local humanoid = part.Parent:FindFirstChild("Humanoid")
    if humanoid then
        block.BrickColor = BrickColor.new("Bright red")
        local text = Instance.new("BillboardGui", block)
        text.Adornee = block
        text.Size = UDim2.new(1, 0, 1, 0)
        text.StudsOffset = Vector3.new(0, 2, 0)
        local label = Instance.new("TextLabel", text)
        label.Size = UDim2.new(1, 0, 1, 0)
        label.Text = messages[math.random(#messages)]
        label.TextSize = label.TextSize + 10
        label.TextColor3 = Color3.new(1, 1, 1)
        label.BackgroundTransparency = 1
        wait(3)
        block.BrickColor = originalColor
        text:Destroy()`your text`
    end
end

block.Touched:Connect(onTouched)

答案1

得分: 1

以下是您要翻译的内容:

问题在于当您走过该部分时,触摸事件可能会多次触发。为了防止这种情况,您可以使用通常被称为“防抖”的方法,这可以防止事件过于频繁地触发。

通过检查防抖是否为假,可以防止事件在短时间内频繁触发。

local debounce = false

local function onTouched(part)
    local humanoid = part.Parent:FindFirstChild("Humanoid")
    if humanoid and not debounce then
        block.BrickColor = BrickColor.new("Bright red")
        local text = Instance.new("BillboardGui", block)
        text.Adornee = block
        text.Size = UDim2.new(1, 0, 1, 0)
        text.StudsOffset = Vector3.new(0, 2, 0)
        local label = Instance.new("TextLabel", text)
        label.Size = UDim2.new(1, 0, 1, 0)
        label.Text = messages[math.random(#messages)]
        label.TextSize = label.TextSize + 10
        label.TextColor3 = Color3.new(1, 1, 1)
        label.BackgroundTransparency = 1
        debounce = true
        wait(3)
        block.BrickColor = originalColor
        text:Destroy()
        
        debounce = false
    end
end
英文:

The problem here is that as you walk over the part, the touched event can fire multiple times. To prevent this you can use something that is often called 'debounce' this prevents the event from being fired too often.

By checking if debounce is false, it prevents the event from being fired in quick succession.

local debounce = false

local function onTouched(part)
	local humanoid = part.Parent:FindFirstChild("Humanoid")
	if humanoid and not debounce then
		block.BrickColor = BrickColor.new("Bright red")
		local text = Instance.new("BillboardGui", block)
		text.Adornee = block
		text.Size = UDim2.new(1, 0, 1, 0)
		text.StudsOffset = Vector3.new(0, 2, 0)
		local label = Instance.new("TextLabel", text)
		label.Size = UDim2.new(1, 0, 1, 0)
		label.Text = messages[math.random(#messages)]
		label.TextSize = label.TextSize + 10
		label.TextColor3 = Color3.new(1, 1, 1)
		label.BackgroundTransparency = 1
		debounce = true
		wait(3)
		block.BrickColor = originalColor
		text:Destroy()
		
		debounce = false
	end
end

huangapple
  • 本文由 发表于 2023年4月13日 19:30:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76004902.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定