-
What do you want to achieve?
I’m trying to pass a throttle value through an event. -
What is the issue?
The script isn’t passing through the correct information. -
What solutions have you tried so far?
I’ve tried looking it up of the Developer forum, couldn’t find anything, I also tried swapping the variables but that didn’t work either.
Here’s an image of the output to show what its giving the server script.
Also, I created a test variable called “char” to see how it would react.
Local script:
local RunService = game:GetService("RunService")
local throttle = script.Parent.Parent.Parent:GetAttribute("throttleInput")
local currentThrottle = script.Parent.Parent.Parent:GetAttribute("currentThrottle")
local uInputService = game:GetService("UserInputService")
-- code is structured logic. be descriptive with variable name scheme, you want
-- your code to be easily maintainable. 'uInputService' v. 'usp'
local This = script.Parent; -- this is something you absolutely must get the
-- hang of. Repetitively relying on absolute paths is a clear sign of a novice
-- which is undesirable for even the freshest of learners to the eldest of such.
local ThatFirstParent = This.Parent;
local ThatSecondParent = ThatFirstParent.Parent;
local ThrottleDirection = ThatSecondParent:GetAttribute("Throttle") or 0
local ThrottleValue = ThatSecondParent:GetAttribute("currentThrottle") or 0
local MaxThrottle = 100
local ThrottleIncPerSecond = 20
local changeAttributeEvent = game.ReplicatedStorage:WaitForChild("updateAttribute")
local KeysDown = {
LShift = false,
LCtrl = false
} -- this will be used later on.
-- now this is where it gets from light teaching to a full lecture, read along
-- and I'll teach you why your original code will never,
-- yes, I repeat; never work.
local LeftShiftKey = Enum.KeyCode.LeftShift -- first tidy up these, not needed but
-- very nice and easy to maintain.
local LeftControlKey = Enum.KeyCode.LeftControl
local function HandleKeyPressed(Input, Handled)
if Handled then return end -- you do not want your control keybinds to fire
-- if a textbox is being typed in, or if the user presses a button.
local Key = Input.KeyCode
-- Now, I will demonstrate everything that you need to do.
if Key == LeftShiftKey then
KeysDown.LShift = true -- key buffer so we can make accurate calculations.
elseif Key == LeftControlKey then
KeysDown.LCtrl = true -- see above comment
end
end
local function HandleKeyReleased(Input, Handled)
if Handled then return end -- same ordeal as the previous function; typing/button = halt user input
local Key = Input.KeyCode
-- Now, I will demonstrate everything that you need to do on this part.
if Key == LeftShiftKey then
KeysDown.LShift = false -- upon release we will set the buffer values false
elseif Key == LeftControlKey then
KeysDown.LCtrl = false -- see above comment
end
end
local function UpdateThrottleValue(Deltatime)
ThrottleDirection = (KeysDown.LShift and 1 or 0) + (KeysDown.LCtrl and -1 or 0)
local Current = ThrottleValue; -- hold our current state
local Goal = Current + (ThrottleIncPerSecond * ThrottleDirection)
ThrottleValue = math.clamp((1-Deltatime) * Current + (Deltatime * Goal), 0, MaxThrottle)
local char = nil
-- Let me explain the above line.
-- The formula here is 1 - Alphatime * Start + Alphatime * Goal
-- Also known as, linear interpolation! (lerp)
-- Alphatime is an arbitrary term for: It can be anything from range0 to range1; 0-1 in this case
-- inline function: function(x0,x1,a) return (1-a)*x0+(a*x1) end
-- Anyways, this could've been simplified, but I'm doing this for learning purposes.
-- I hope this has taught you something, if not; sad boy hours I suppose.
changeAttributeEvent:FireServer(char, ThrottleValue)
end
RunService.Heartbeat:Connect(UpdateThrottleValue)
local InputBegan = uInputService.InputBegan:Connect(HandleKeyPressed)
local InputEnded = uInputService.InputEnded:Connect(HandleKeyReleased)
-- Oh right, and finally we connect the update function to Heartbeat; and connect our input handlers!
-- EDIT: ThrottleValue is the equivalent variable to currentThrottle
-- QuickFIX: Forgot to clamp the value! Fixed that, MaxThrottle is the correspondent max value!
Server script:
local this = script.Parent.Parent
local throttle = script.Parent.Parent:GetAttribute("throttleInput")
local currentThrottle = script.Parent.Parent:GetAttribute("currentThrottle")
local ObjectWithAttribute = script.Parent.Parent
local updateAttribute = Instance.new("RemoteEvent")
updateAttribute.Name = "updateAttribute"
updateAttribute.Parent = game.ReplicatedStorage
updateAttribute.OnServerEvent:Connect(function(char, ThrottleValue)
print(ThrottleValue, char)
currentThrottle = char
ObjectWithAttribute:GetAttributeChangedSignal("currentThrottle"):Connect(function()
ObjectWithAttribute.BodyVelocity.Velocity = ObjectWithAttribute.CFrame.LookVector * currentThrottle
end)
end)
(By the way, the “instance.new” for the event creation will be changed eventually)
(Oh, also the client script is an edited one from a different forum I posted that’s why it has the notes)
Thanks,
– Luke