Hi Developers,
I am trying to send a NumberValue from Client to Server-side , However I am given the “value of type Instance cannot be converted to a number” error, is there a way to fix this?
Thanks.
Code from client side:
local Trainvalues = script.Parent.ObjectValue.Value
Trainvalues.Values.Speed.Changed:Connect(function()
speed:FireServer(Trainvalues.Values.Speed.Value)
end)
Code from server side:
local connection = script.Parent.Parent.Parent.Value.Value
script.Parent.OnServerEvent:Connect(function(Player, info)
connection.Values.Speed.Value = info
end)
A bit more context on all the indexes might be needed here, are the Values a child of the ObjectValue or are they a child of the ObjectValue’s stored object?
Based on this:
local Trainvalues = script.Parent.ObjectValue.Value - this redirects to the object itself that is stored in the value, meaning that if the values arent a child of that specific object that the ObjectValue is storing, then you are indexing to nothing
local Model = ObjectValue.Value --The value is the model, stored inside the ObjectValue
local NumberValue = Instance.new("NumberValue",ObjectValue) --Creates a number value and parents it to the ObjectValue
Model.NumberValue.Value = 5 --Doesnt work because it's a child of the ObjectValue, not of the Model
Don’t name your _Value instances just Value cause they actually have a property called Value which corresponds to the actual value / reference (if its ObjectValue)
so either rename your NumberValue to something else or do Value.Value instead
local connection = script.Parent.Parent.Parent.Value.Value
script.Parent.OnServerEvent:Connect(function(Player, info)
connection.Values.Speed.Value = info
end)
You would need
local connection = script.Parent.Parent.Parent.Value.Value
script.Parent.OnServerEvent:Connect(function(Player, info)
connection.GUI2.Values.Speed.Value = info
end)
Edit: Same for the client version in case that is indexed wrongly too
You are trying to send the value of an ObjectValue instance to the server, and trying to convert the value stored by that ObjectValue instance into a number, which is impossible.