【问题标题】:Why are the Color3 Values changing to the wrong number?为什么 Color3 值更改为错误的数字?
【发布时间】:2020-09-11 16:01:21
【问题描述】:

我正在努力使其成为玩家可以进行自定义的地方,但是当它设置 Color3 值时,它会更改为比正常参数大得多的数字。例如,当脚本尝试将值设置为 (255,0,0) 时,它会转到 (65025, 0, 0)。这是代码,我做错了什么?

script.Parent.MouseButton1Click:Connect(function()
    local Player = script.Parent.Parent.Parent.Parent.Parent.Parent
    print("CustomChanged")
    if script.Parent.Value.Value == Color3.new(0,0,0) then
        script.Parent.Value.Value = Color3.new(255,255,255)
        Player.Skins.Custom.Head.Color.Value = script.Parent.Value.Value
        script.Parent.Parent.Parent.Character.Custom.Head.Color = script.Parent.Value.Value
        script.Parent.Frame.BackgroundColor3 = script.Parent.Value.Value
    elseif script.Parent.Value.Value == Color3.new(255,255,255) then
        script.Parent.Value.Value = Color3.new(255,0,0)
        Player.Skins.Custom.Head.Color.Value = script.Parent.Value.Value
        script.Parent.Parent.Parent.Character.Custom.Head.Color = script.Parent.Value.Value
        script.Parent.Frame.BackgroundColor3 = script.Parent.Value.Value
    elseif script.Parent.Value.Value == Color3.new(255,0,0) then
        script.Parent.Value.Value = Color3.new(255, 136, 0)
        Player.Skins.Custom.Head.Color.Value = script.Parent.Value.Value
        script.Parent.Parent.Parent.Character.Custom.Head.Color = script.Parent.Value.Value
        script.Parent.Frame.BackgroundColor3 = script.Parent.Value.Value
    elseif script.Parent.Value.Value == Color3.new(255, 136, 0) then
        script.Parent.Value.Value = Color3.new(255,255,0)
        Player.Skins.Custom.Head.Color.Value = script.Parent.Value.Value
        script.Parent.Parent.Parent.Character.Custom.Head.Color = script.Parent.Value.Value
        script.Parent.Frame.BackgroundColor3 = script.Parent.Value.Value
    elseif script.Parent.Value.Value == Color3.new(255,255,0) then
        script.Parent.Value.Value = Color3.new(0,255,0)
        Player.Skins.Custom.Head.Color.Value = script.Parent.Value.Value
        script.Parent.Parent.Parent.Character.Custom.Head.Color = script.Parent.Value.Value
        script.Parent.Frame.BackgroundColor3 = script.Parent.Value.Value
    elseif script.Parent.Value.Value == Color3.new(0,255,0) then
        script.Parent.Value.Value = Color3.new(0,255,255)
        Player.Skins.Custom.Head.Color.Value = script.Parent.Value.Value
        script.Parent.Parent.Parent.Character.Custom.Head.Color = script.Parent.Value.Value
        script.Parent.Frame.BackgroundColor3 = script.Parent.Value.Value
    elseif script.Parent.Value.Value == Color3.new(0,255,255) then
        script.Parent.Value.Value = Color3.new(0,0,255)
        Player.Skins.Custom.Head.Color.Value = script.Parent.Value.Value
        script.Parent.Parent.Parent.Character.Custom.Head.Color = script.Parent.Value.Value
        script.Parent.Frame.BackgroundColor3 = script.Parent.Value.Value
    elseif script.Parent.Value.Value == Color3.new(0,0,255) then
        script.Parent.Value.Value = Color3.new(255,0,255)
        Player.Skins.Custom.Head.Color.Value = script.Parent.Value.Value
        script.Parent.Parent.Parent.Character.Custom.Head.Color = script.Parent.Value.Value
        script.Parent.Frame.BackgroundColor3 = script.Parent.Value.Value
    elseif script.Parent.Value.Value == Color3.new(255,0,255) then
        script.Parent.Value.Value = Color3.new(255,0,205)
        Player.Skins.Custom.Head.Color.Value = script.Parent.Value.Value
        script.Parent.Parent.Parent.Character.Custom.Head.Color = script.Parent.Value.Value
        script.Parent.Frame.BackgroundColor3 = script.Parent.Value.Value
    elseif script.Parent.Value.Value == Color3.new(255,0,205) then
        script.Parent.Value.Value = Color3.new(0,0,0)
        Player.Skins.Custom.Head.Color.Value = script.Parent.Value.Value
        script.Parent.Parent.Parent.Character.Custom.Head.Color = script.Parent.Value.Value
        script.Parent.Frame.BackgroundColor3 = script.Parent.Value.Value
    end
end)

【问题讨论】:

    标签: lua roblox


    【解决方案1】:

    您应该改用Color3.fromRGB(255, 0, 0)

    如果你使用Color3.new(...),它期望颜色值在 0 到 1 之间。

    因此,由于在资源管理器中显示的是该值的 255 倍,因此您会看到 255 * 255 = 65025。

    顺便说一句:

    如果您想避免为每种颜色重复相同的代码,您可以这样做:

    -- store all colors that you want to use in an array
    local colors = {
        Color3.fromRGB(0,0,0),
        Color3.fromRGB(255,255,255),
        Color3.fromRGB(255,0,0),
        Color3.fromRGB(0,255,0),
        Color3.fromRGB(0,0,255)
    }
    
    -- keep track of current color
    currentColor = 1
    
    script.Parent.MouseButton1Click:Connect(function()
        if (currentColor == #colors) then                           -- it current color is the last one in the array, go back to zero
            currentColor = 0
        end
        currentColor = currentColor + 1                             -- increment current color by one
    
        script.Parent.BackgroundColor3 = colors[currentColor]       -- (here you do whatever you want to do with the color)
    end)
    

    【讨论】:

      猜你喜欢
      • 2015-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-13
      • 1970-01-01
      • 2023-03-29
      相关资源
      最近更新 更多