the Vehicle is an object value and I'm trying to set it to the cloned car, but it does not set. Wondering if I should make the value or script in a different location?
script.Parent.ClickDetector.MouseClick:Connect(function(player) local VehicleCount = player.leaderstats.vehicle.Value if VehicleCount then workspace:FindFirstChild(VehicleCount):Destroy() end local car = game.ServerStorage.FordFocus:Clone() local prompt = car.VehiclePrompt:WaitForChild("VehicleDriver") prompt = player.Name car.Parent = workspace VehicleCount = workspace:FindFirstChild(car) end)
You need to reference the ObjectValue itself, you can’t change the .Value
of it when it is set to be a variable.
If you reference the value, then do local value = ObjectValue.Value
, it is “stored” inside the variable, and any changes made to that variable (value = workspace
) will not change the actual property, but only the variable itself.
(Sorry if this makes no sense, bad at explaining this.)
e.g.
local intValue = workspace.IntValue --set to be 1 local intValueValue = intValue.Value --this variable is currently 1 intValueValue = 2 intValue.Value = 0 print(intValueValue) --2 print(intValue.Value) --0
So your script should look something like this instead:
script.Parent.ClickDetector.MouseClick:Connect(function(player) local VehicleCount = player.leaderstats.vehicle if VehicleCount.Value then workspace:FindFirstChild(VehicleCount.Value):Destroy() end local car = game.ServerStorage.FordFocus:Clone() local prompt = car.VehiclePrompt:WaitForChild("VehicleDriver") prompt = player.Name car.Parent = workspace VehicleCount.Value = workspace:FindFirstChild(car) end)