Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Object Value not being set?

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)

1 answer

✓ Accepted answer

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)

2 comments

0

Hi! Thank you so much for answering! It writes to the object value, but now

0

Oops- forgot to finish writing the comment but i fixed it haha

This discussion is preserved as it was. New answers, comments, and votes are closed.