报错“Text is not a valid member of Frame”,Roblox滑块脚本如何修复?
问题解决:"Text is not a valid member of Frame" 错误及滑块显示逻辑修复
核心问题解析
控件类型错误
错误提示明确说明你调用Text属性的对象是Frame,但Frame没有Text属性。你的变量WalkSpeedDisplay = SliderFrame.WalkSpeedNum指向的是一个Frame控件,而非用于显示文本的TextLabel或TextBox。显示逻辑错误
你当前用game.StarterPlayer.CharacterWalkSpeed作为显示值,这是角色的初始默认移动速度,并非滑块调节后的实时速度,所以即使控件类型正确,显示内容也不会随滑块变化。
修复步骤
步骤1:修正UI控件类型
在Roblox Studio中找到 SliderFrame 下的 WalkSpeedNum 对象:
- 将其类型从
Frame改为TextLabel(仅用于显示推荐用这个)或TextBox。 - 确保对象名称正确无误,脚本能正确引用到它。
步骤2:修正脚本逻辑
修改脚本中的两处问题:
- 删除重复定义的
local SliderFrame = script.Parent.WalkSpeedSlider(后面重新定义了一次,重复变量无意义)。 - 替换显示值为滑块调节后的实际移动速度,而非默认值。
- 增加Humanoid空值判断,避免角色未加载时出错。
修复后的完整代码
--SLIDER: local UIS = game:GetService("UserInputService") local WalkSpeedSlider = script.Parent.WalkSpeedSlider local SliderFrame = WalkSpeedSlider.SliderFrame local Slider = SliderFrame.Slider local WalkspeedReset = SliderFrame.Reset local WalkspeedDragging = false local WalkSpeedDisplay = SliderFrame.WalkSpeedNum -- 确保这个对象是TextLabel/TextBox Slider.MouseButton1Down:Connect(function() WalkspeedDragging = true end) UIS.InputEnded:Connect(function(input) if input.UserInputType == Enum.UserInputType.MouseButton1 then WalkspeedDragging = false end end) UIS.InputChanged:Connect(function(input) if input.UserInputType == Enum.UserInputType.MouseMovement and WalkspeedDragging then local MouseL = UIS:GetMouseLocation() local RelativePos = MouseL - SliderFrame.AbsolutePosition local percent = math.clamp(RelativePos.X / SliderFrame.AbsoluteSize.X, 0, 1) Slider.Position = UDim2.new(percent, 0, 0, 0) -- 获取当前玩家的Humanoid并修改移动速度 local humanoid = game.Players.LocalPlayer.Character:FindFirstChildWhichIsA("Humanoid") if humanoid then local newWalkSpeed = math.round(percent * 120) humanoid.WalkSpeed = newWalkSpeed -- 用实际修改后的速度更新显示 WalkSpeedDisplay.Text = tostring(newWalkSpeed) end end end)
额外提示
- 调试时可以用
print(typeof(WalkSpeedDisplay))确认控件类型,确保是TextLabel/TextBox。 - 如果需要保留初始值重置功能,可以在
WalkspeedReset的点击事件中恢复Humanoid的WalkSpeed为game.StarterPlayer.CharacterWalkSpeed,并同步更新显示。
内容的提问来源于stack exchange,提问作者Mighty
相关产品推荐
相关产品推荐

