Function Parameter "Axis" Not Working

Any ideas?

local PlayerPosition = game.Workspace:WaitForChild(PlayerName).HumanoidRootPart.Position

local HardGoal = game.Workspace:WaitForChild('Finish')

local function GetDistance(GoalPart, Axis)
		local Distance = PlayerPosition.Axis - GoalPart.Position.Axis
		if Distance > -10 and Distance < 10 then
			Active = false
		end
	end
end

GetDistance(HardGoal, Z)

use a string

local PlayerPosition = game.Workspace:WaitForChild(PlayerName).HumanoidRootPart.Position

local HardGoal = game.Workspace:WaitForChild('Finish')

local function GetDistance(GoalPart, Axis)
	local Distance = PlayerPosition[Axis] - GoalPart.Position[Axis]
	if Distance > -10 and Distance < 10 then
		Active = false
	end
	
end

GetDistance(HardGoal, "Z")

I got the error "Players.Turned_Away.PlayerScripts.StudsLeft:14: invalid argument #2 (string expected, got nil) "

can you show me what line that is? it’s not something in the script you provided

I replaced

local Distance = PlayerPosition.Z - GoalPart.Position.Z

with

local Distance = PlayerPosition[Axis] - GoalPart.Position[Axis]

are you passing a string (string is a text inside " ") as an argument when calling the function?

GetDistance(HardGoal, "Z")
1 Like

I wasn’t but now the local script is deleting itself for some reason and not running at all, will try this in a separate script and see if it works

is the local script inside workspace? if yes you should know that local scripts dont run in the workspace and serverscriptservice.

It was running earlier in StarterPlayerScripts but now it’s not, testing your fix in a diff script rn though

1 Like

You were right, this was the fix. Here’s the test I used for reference:

local part = script.Parent

local function Number(Axis)
	local x = part.Position[Axis]
	print(x)
end

Number('X')
1 Like

glad that i helped

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