Hi
I have a script in a part (called ship) which uses a tween to move it between point A and B. The ship successfully executes the tween. Here is the code:
local part = script.Parent local TweenService = game:GetService("TweenService") local sapphirePosition = workspace.ores.sapphire.Position while true do travel_time = workspace.ships.sapphire_collector.ship.movement_script.travel_time.Value local info = TweenInfo.new( travel_time, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut, 0, true, 0 ) local goal = {Position = Vector3.new(sapphirePosition.X, sapphirePosition.Y+3, sapphirePosition.Z);} local runTween = TweenService:Create(part, info, goal) runTween:Play() print(travel_time) wait(travel_time*2) wait() end
I also have a LocalScript inside a TextButton inside a ScreenGUI inside the StarterGUI folder. I use this GUI button to increment a NumberValue called travel_time by 1. This NumberValue is a child of the ship's tween script. The GUI successfully increments the NumberValue. Here is the GUI's script:
local button = script.Parent local travel_time = workspace.ships.sapphire_collector.ship.movement_script.travel_time.Value function onButtonActivated() travel_time = travel_time+1 workspace.ships.sapphire_collector.ship.movement_script.travel_time.Value = travel_time print(travel_time) end button.Activated:Connect(onButtonActivated)
The problem: When I click the button to increase the travel_time NumberValue, it appears the ship's script isn't seeing the changed value and the tween's "time" property isn't increasing, even though I've set it to recheck the NumberValue each time the tween is played.
In other words: The NumberValue is definitely incrementing correctly, but the tween script isn't seeing it and only prints the default value set by the NumberValue which is 2.
I'm probably overlooking something with my lack of experience. Any thoughts?
EDIT 2:
LocalScript:
local Button = script.Parent local TravelTime = workspace.Ships.SapphireCollector.Ship.MovementScript.TravelTime.Value local ReplicatedStorage = game:GetService("ReplicatedStorage") -- retrieve the ReplicatedStorage instance local UpdateEvent = ReplicatedStorage.UpdateValue -- store the UpdateValue event as a variable function onButtonActivated() UpdateEvent:FireServer(TravelTime) -- sends a message to the server through the RemoteEvent end Button.Activated:Connect(onButtonActivated)
Server Script:
local Part = script.Parent local TweenService = game:GetService("TweenService") local SapphirePosition = workspace.Ores.Sapphire.Position local ReplicatedStorage = game:GetService("ReplicatedStorage") local UpdateEvent = ReplicatedStorage.UpdateValue UpdateEvent.OnServerEvent:Connect(function(player,TravelTime) -- receiver for the RemoteEvent TravelTime += 1 print(TravelTime) end) while true do TravelTime = workspace.Ships.SapphireCollector.Ship.MovementScript.TravelTime.Value local Info = TweenInfo.new( TravelTime, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut, 0, true, 0 ) local Goal = {Position = Vector3.new(SapphirePosition.X, SapphirePosition.Y+3, SapphirePosition.Z)} local RunTween = TweenService:Create(Part, Info, Goal) RunTween:Play() print(TravelTime) wait(TravelTime*2) wait() end
Now my TravelTIme NumberValue won't update. Can't figure out why :/
LocalScripts deal with the client and Scripts, the server. LocalScripts handle everything on the user's side while Scripts handle everything on the server's side. The server cannot see what's happening on the client, so in your case, the server cannot see that the LocalScript has changed travel_time
's value. You can read more on the difference between LocalScripts and Scripts as well as how the Client-Server Model works here.
As for solving your issue, the solution would be to make the server change travel_time
's value instead of the client. To do that, we will use a RemoteEvent. A RemoteEvent is how the client communicates with the server and vice-versa.
Let's insert a RemoteEvent into ReplicatedStorage and name it Update_Value. ReplicatedStorage is a container service that both the client and server can access.
We'll setup the LocalScript first:
local button = script.Parent local travel_time = workspace.ships.sapphire_collector.ship.movement_script.travel_time local ReplicatedStorage=game:GetService('ReplicatedStorage') -- retrieve the ReplicatedStorage instance; GetService(/argument/) requests the service with that name to which it will return with an instance of said name local UpdateEvent=ReplicateStorage.Update_Value -- storing the Update_Value event as a variable for, y'know, safekeeping function onButtonActivated() UpdateEvent:FireServer(travel_time) -- sends a message to the server through the RemoteEvent end button.Activated:Connect(onButtonActivated)
Next, the Script:
local part = script.Parent local TweenService = game:GetService("TweenService") local sapphirePosition = workspace.ores.sapphire.Position local ReplicatedStorage=game:GetService('ReplicatedStorage') local UpdateEvent=ReplicateStorage.Update_Value UpdateEvent.OnServerEvent:Connect(function(player,travel_time) -- receiver for the RemoteEvent travel_time.Value+=1 -- increments by one end) while true do travel_time = workspace.ships.sapphire_collector.ship.movement_script.travel_time.Value local info = TweenInfo.new( travel_time, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut, 0, true, 0 ) local goal = {Position = Vector3.new(sapphirePosition.X, sapphirePosition.Y+3, sapphirePosition.Z)} local runTween = TweenService:Create(part, info, goal) runTween:Play() print(travel_time) wait(travel_time*2) wait() end
You may have noticed that I used += on line 9. This is called an addition assignment operator. It works the same way as writing travel_time.Value=travel_time.Value+1
. You can read on that here. This isn't as important as learning the other stuff, but I'll leave this here just because.