Task.spawn cant access variables?

			local PlayerName = plr.Name
			PlayerClimbing[PlayerName] = true
			
			task.spawn(function()
				print("spawned " .. PlayClimbAnimation[PlayerName].Length)
				task.wait(PlayClimbAnimation[PlayerName].Length - 0.001)
				print("passed " .. PlayerClimbing[PlayerName])--nil
				if PlayerClimbing[PlayerName] then	--PLAYERCLIMBING IS CONSIDERED NIL EVEN IF IT'S DECLARED TRUE BEFORE
					local savePos = PlayerTorso.Position
PlayerTorso.Position = savePos
				end
			end)

when i declare on PlayerClimbing table a player’s value to true and check it on the task.spawn, inside of it the variable is always nil, any idea of how to check it? (playerclimbing value can change on the middle of the Wait’s)

2 Likes

maybe stuff the PlayerClimbing variable into the parameter of the function inside task.spawn?

task.spawn(function(PlayerClimbing)
				print("spawned " .. PlayClimbAnimation[PlayerName].Length)
				task.wait(PlayClimbAnimation[PlayerName].Length - 0.001)
				print("passed " .. PlayerClimbing[PlayerName])--nil
				if PlayerClimbing[PlayerName] then	--PLAYERCLIMBING IS CONSIDERED NIL EVEN IF IT'S DECLARED TRUE BEFORE
					local savePos = PlayerTorso.Position
PlayerTorso.Position = savePos
				end
			end)
1 Like


local PlayerClimbing = {}
PlayerClimbing is a Table, and i dont know what putting it into task.spawn does exactly, i guess it creates a nil variable??

1 Like

oh yea right i forgot

local PlayerClimbing = {
	Yes = "no",
}

PlayerClimbing["Cheff"] = 123

task.spawn(function()
	print(PlayerClimbing)
end)

but this works fine for me
image

which means task.spawn can access variables so you might got some other problems in your script

and if you want to know the right way to pass parameters into task.spawn, it’s this way:

task.spawn(function(msg, ...)
	print(msg, ...) -- will print "hello hot dog"
end, "hello", "hot", "dog")

Couldn’t find an exact reason, i guess this was just an engine bug, code worked normally after a few minutes and studio resetting without any change

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