Why is the script not working?

Why is this script not working?

1 Like

Can you please explain what you’re trying to achieve?

The magnitude of the part and the player.

1 Like

There is a couple of things that I can see wrong with this code that could be causing the script not to work. First of all it looks like you are running this code in a LocalScript in workspace. The problem with this is that LocalScripts don’t run in workspace. The other problem I can see is that you haven’t defined ice anywhere so that could be causing something to error.

It would be nice to know what your specific use cause for this would be. When do specifically need to know the magnitude of the part and the player? Is this script or a LocalScript and where is it located? Is this the full code? Knowing this information will help use give you a better answer and we may be able to give you a better solution to what you are trying to achieve.


In the future could you please follow the scripting support guidelines when creating new topics because it is really hard to just fix a script with very limited information:

1 Like

I think your script isn’t working because it couldn’t get the HumanoidRootPart (Probably it didn’t exist at the time the script was running so it returned “nill”), or
ice.Position isn’t a defined variable.

Anyways her is the working version of your script.

  • This was in a LocalScript.
local Player = game:GetService("Players").LocalPlayer
local Hrp = game.Workspace:FindFirstChild(Player.Name).HumanoidRootPart
local Part = game.Workspace.Part

while wait(1) do
	print((Part.Position - Hrp.Position).magnitude)
end

Hope this helps you and answers your question! :wink:

1 Like

This is because your code is being ran in a script and not a LocalScript. You can only use game.Player.LocalPlayer in a LocalScript located somewhere on the client.

2 Likes

I will recommend having this in StarterCharacterScripts

There’s not a nothing wrong in the output but it doesn’t say the magnitude still.

Did you test my code and it didn’t print the magnitude?
It did work with me when it was placed in StarterCharacterScript as a LocalScript

Yes I tested your script.
It didn’t work.

Send the output and where you have it in the Explorer

image

The reason why this isn’t working is because LocalScripts can’t run in workspace. Only server scripts can. LocalScripts can only run on the client.

@waterrunner already mentioned that you can’t have LocalScript in workspace.

But then i can’t use “game.Player.LocalPlayer”

Having the LocalScript in StarterCharacterScript is just as having it as a child of the part. It will do the same task

Try having the same exact script in StarterCharacterScript as a LocalScript for example.

1 Like

It worked, thank you!
Should have done it in the first place.

1 Like

Mark the solution to end this topic so it is easier for people to find the solution rather than getting lost in this :happy3: