Changing values in OOP

So i am new to OOP, especially in lua and im just generally trying to use it wherever i can. The problem is that right now, it seems that i cant change a value of a variable i stored in an object. Also, slightly unrelated but how do you add variables to an object whilst not putting them in the curly brackets?

Creaturedata = {preference = 0,currentpos = nil,nextpos = nil,speedval = 20} 
-- The creation of the object

Box.Touched:Connect(function(hit,Creaturedata)
	wait(0.2)
	if hit.Parent == character then 
		while task.wait(0.5) do
			local raystart = chosencreature.CFrame.Position
			local rayend = workspace.Camera.CFrame.Position
			local raydirect = rayend - raystart
			local raycast = workspace:Raycast(raystart,raydirect)
			print("Distance:", raycast.Distance)
			Creaturedata.speedval = raycast.Distance/2 -- This is the part that errors, and says that speedval doesn't exist
		end
	end
end)

Am i doing something wrong?

2 Likes

You aren’t changing the value correctly: tables work differently:

Creaturedata.speedval = raycast.Distance/2
-- turns into:
Creaturedata[4] = raycast.Distance/2

No? OP is clearly using a key-value pair, not an array.
Indexing it with a number would be nil.

1 Like

Setting Creaturedata[4] to a value would result in something like this:

local Creaturedata = {
    [4] = raycast.Distance/2
}

Apologies, in that case you do the following:

Creaturedata.speedval = raycast.Distance/2
-- turns into:
Creaturedata["speedval"] = raycast.Distance/2

Sorry if this still isn’t correct, I’m new to these forums and still not that good at coding myself

btw you’re defining a new Creaturedata in the parameters of the connection, which might not be the table what you’re looking for.

1 Like

Actually, Creaturedata isn’t even a valid parameter.
.Touched only returns the hit: BasePart, not anything else.

Box.Touched:Connect(function(hit: BasePart): ()
    --// ...
end
1 Like

They both are equivalent. You can index with both the dot and [] operators.

Then the problem lies in the parameter you mentioned earlier. Thanks for teaching both OP and me something new! LOL

As they said before, Touched only returns one value, if you try to enter another value it will be nil, hence the error.

attempt to index nil with speedval 

Also raycast can give nil sometimes, so it is better to check it:

Creaturedata = {preference = 0,currentpos = nil,nextpos = nil,speedval = 20} 
-- The creation of the object

Box.Touched:Connect(function(hit)
	task.wait(0.2)
	if hit.Parent == character then 
		while task.wait(0.5) do
			local raystart = chosencreature.CFrame.Position
			local rayend = workspace.Camera.CFrame.Position
			local raydirect = rayend - raystart
			
			local raycast = workspace:Raycast(raystart,raydirect)
			if raycast then
				print("Distance:", raycast.Distance)
				Creaturedata.speedval = raycast.Distance/2
			else
				warn("hey, no raycast")
			end
		end
	end
end)
2 Likes

Thank you guys, I honestly have no idea what I’m doing and this really helped

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.