Hi,everyone.
I am creating a GUI that displays the velocity when sitting on a Vehicleseat.
I want to display the value of Value.Value in the TextLabel of ScreenGui, but it is not updated dynamically.
It is updated every time I sit on the Vehicleseat again.
Can you please tell me what is wrong with my script?
↓Script
while true do
wait()
script.Parent.DriveGui.Speed.TextLabel.Text = script.Parent.DriveGui.spd.Value
end
You don’t have to loop your script everytime, this could be problematic and inefficient. Instead,
You could use .Changed event to run a function everytime a value, etc is changed:
I wrote the following in LocalScript, but it does not work.
GuiScript is also included for reference.
local seatGui = script.Parent:FindFirstChild("DriveGui")
local Players = game:GetService("Players")
local CurrentOccupant = nil
script.Parent:GetPropertyChangedSignal("Occupant"):Connect(function()
if script.Parent.Occupant then
local plr = Players:GetPlayerFromCharacter(script.Parent.Occupant.Parent)
CurrentOccupant = plr
local plrGui = plr:FindFirstChild("PlayerGui")
if plrGui then
local guiClone = seatGui:Clone()
guiClone.Parent = plrGui
end
else
local verify = CurrentOccupant:FindFirstChild("PlayerGui")
if verify then
local Object = CurrentOccupant.PlayerGui:FindFirstChild("DriveGui")
Object:Destroy()
CurrentOccupant = nil
else
CurrentOccupant = nil
end
end
end)
local spd = script.Parent.spd.Value
function speedc()
local acc = script.Parent.acc.Value
local brk = script.Parent.brk.Value
local notch = script.Parent.DriveGui.noc.Value
if 0 < notch then
script.Parent.DriveGui.spd.Value = (acc / 4 * notch) + script.Parent.DriveGui.spd.Value
elseif 0 > notch then
if script.Parent.DriveGui.spd.Value > 0 then
script.Parent.DriveGui.spd.Value = script.Parent.DriveGui.spd.Value - (brk / 7 * notch * -1)
end
end
if script.Parent.DriveGui.spd.Value < 0 then
if notch < 0 then
script.Parent.DriveGui.spd.Value = 0
end
end
end
while true do
speedc()
wait()
end
The data you put into spd, well send it through the RemoteEvent
Copy new code and replace old code
Script
-- Script
local RemoteEvent = game.ReplicatedStorage.RemoteEvent
local function SendDataToClient(data)
local player = "Put player here" -- Put the Player here
RemoteEvent:FireClient(player, data)
end
local spd = script.Parent.spd.Value
function speedc()
local acc = script.Parent.acc.Value
local brk = script.Parent.brk.Value
local notch = script.Parent.DriveGui.noc.Value
if 0 < notch then
local data = (acc / 4 * notch) + script.Parent.DriveGui.spd.Value
SendDataToClient(data)
elseif 0 > notch then
if script.Parent.DriveGui.spd.Value > 0 then
local data = script.Parent.DriveGui.spd.Value - (brk / 7 * notch * -1)
SendDataToClient(data)
end
end
if script.Parent.DriveGui.spd.Value < 0 then
if notch < 0 then
local data = 0
SendDataToClient(data)
end
end
end
while true do
speedc()
wait()
end
LocalScript
Replace the above code with the following
-- LocalScript
local RemoteEvent = game.ReplicatedStorage.RemoteEvent
RemoteEvent.OnClientEvent:Connect(function(data)
script.Parent.Speed.TextLabel.Text = data
end)
I tried to test the code you provided, but unfortunately it did not work in my environment.
I searched the forum and only found examples using RemoteEvents, so I will take your advice and investigate further.
digression
bin = script.Parent
eng = { }
st = bin.VehicleSeat
msp = script.MaximumSpeed.Value
st.MaxSpeed = msp
mrs = script.MaximumReverseSpeed.Value --> Maximum Reverse Speed
spt = script.SpeedPerThrottle.Value --> Speed Per Throttle. This is how much each notch on the throttle changes
mxt = math.ceil(msp / spt) --> This is the maximum throttle
crt = 0 --> Current throttle
csd = 0 --> Current speed
cdr = 0 --> Current direction
drive = false --> Is the train driving. False = not driving, true = driving
plr = nil --> This will change to the current driver
DirTable = {"Reverse", "Neutral", "Foward"}
TrtStp = 0 --> This is used so there is a second or 2 gap in between changing the throttle.
function GetEngines() --> This finds all the engines in the train and then puts them into the table 'eng'
for _, a in pairs(bin:GetChildren()) do
if (a.Name == "Engine") then
--if a:findFirstChild("BodyVelocity") then
local bv = script.BodyVelocity:Clone()
bv.Parent = a
eng[#eng + 1] = a
--end
end
end
end
function GetDirString() return DirTable[cdr + 2]; end
function GetSpeedString(a)
if (a < 0) then return "-"..a.."" end
return msp
end
function GetPlayer()
local a = st.SeatWeld.Part1
if a then
local p = game.Players:findFirstChild(a.Parent.Name)
if p then
return p
end
end
return false
end
function GetHint(p)
-- local a = p:findFirstChild("TrainDriveHint")
local a = p.PlayerGui:findFirstChild("TrainDriveGui")
if a then return a; end
return
end
wait(1)
GetEngines()
h = script.TrainDriveGui:Clone()
h.TextDisplay.Text = "Direction: "..GetDirString().." - Throttle: "..crt.."/"..mxt.." - Speed: "..GetSpeedString(csd).."/"..GetSpeedString(mrs)
h2 = h:Clone();
st.ChildAdded:connect(function ()
drive = true
--[[local cam = script.Cam:Clone()
cam.Disabled = false
if GetPlayer() then
cam.Parent = GetPlayer().Backpack
end]]
end)
st.ChildRemoved:connect(function ()
drive = false
if GetHint(plr) then
GetHint(plr).Parent = nil
h = script.TrainDriveGui:Clone()
h2 = h:Clone()
end
end)
while true do
wait(0.05)
if drive then
local p = GetPlayer()
if p then
plr = p
local gh = GetHint(p)
if not gh then h2.Parent = p.PlayerGui end
local h2 = GetHint(p)
--Now to calculate the current throttle
local trt, dir = st.Steer, st.Throttle
if (TrtStp > 0) then
TrtStp = TrtStp - 0.5
else
if (trt == -1) then
if (crt > 0) then crt = crt - 1; TrtStp = 1; end
elseif(trt == 1) then
if (crt < mxt) then crt = crt + 1; TrtStp = 1; end
end
end
--Throttle calculated, now get the direction
cdr = cdr + dir
if (cdr > 1) then cdr = 1 end
if (cdr < -1) then cdr = -1 end
--Direction done, now work out the speed
if (crt == 0) then
if (csd < 0) then
if (csd + 0.5 > 0) then
csd = 0
else
csd = csd + 0.5
end
elseif (csd > 0) then
if (csd - 0.5 < 0) then
csd = 0
else
csd = csd - 0.5
end
end
end
if (crt > 0) then
if (cdr == -1) then
if (csd - (crt / 10) < (spt * crt) * -1) then
csd = (spt * crt) * -1
else
csd = csd - (crt / 10)
end
csd = csd - (crt / 10)
if (csd < (spt * crt) * -1) then csd = csd + 0.5 end
if (csd < (mrs * -1)) then csd = mrs * -1 end
if (csd > 0) then csd = csd * -1 end
elseif (cdr == 1) then
if (csd + (crt / 10) > spt * crt) then
csd = spt * crt
else
csd = csd + (crt / 10)
end
if (csd > spt * crt) then csd = csd - 0.5 end
if (csd > msp) then csd = msp end
if (csd < 0) then csd = csd * -1 end
end
end
for _, a in pairs(eng) do
a.BodyVelocity.velocity = a.CFrame.lookVector * csd
end
h2.TextDisplay.Text = "Direction: "..GetDirString().." - Throttle: "..crt.."/"..mxt.." - Speed: "..csd.."/"..GetSpeedString(mrs)..""
end
end
end
This is a script I got from a library, but it works perfectly without using LocalScript.
It’s strange
Simple explanation of the differences between using RemoteEvent and the code you provided.
Using RemoteEvent
The first parameter in :FireClient( ) is the player, who you want to send it to.
Second parameter in :FireClient( ) is data, data we want to send to the player’s localscript.
-- Script
local RemoteEvent = game.ReplicatedStorage.RemoteEvent
local player = -- Put player here
local data = "hello"
RemoteEvent:FireClient(player, data)
This line of code assumes there is gui with a Textlabel in it “script.Parent.Speed.TextLabel.Text”
-- LocalScript
local RemoteEvent = game.ReplicatedStorage.RemoteEvent
RemoteEvent.OnClientEvent:Connect(function(data)
script.Parent.Speed.TextLabel.Text = data
end)
Code you provided
Summary
you cloned once then always updating the existing one
First you cloned TrainDriveGui. 57. h = script.TrainDriveGui:Clone()
Then cloned again. 59. h2 = h:Clone();
You got the player that’s driving. 82. local p = GetPlayer()
Find TrainDriveGui. 85. local gh = GetHint(p)
If TrainDriveGui is not found, parent cloned TrainDriveGui into PlayerGui. 86. if not gh then h2.Parent = p.PlayerGui end
If TrainDriveGui is found, assign to h2. 87. local h2 = GetHint(p)
Update TrainDriveGui with new info 144. h2.TextDisplay.Text = "Direction: "..GetDirString().." - Throttle: "..crt.."/"..mxt.." - Speed: "..csd.."/"..GetSpeedString(mrs)..""