Attempt to index nil with "Hit'?

Hello guys,
so I just wanted to make a sphere go to my mouse once but it gives me an error. Did I do anything wrong?


My code:

TweenService:Create(Sphere, TweenInfos, {Position = Mouse.Hit.p}):Play()

The error:


Any help is appreciated! :blush:

1 Like

How has the Mouse variable been declared & initialized?

2 Likes

It’s a script, so it’s trying to get a player’s mouse which doesn’t exist for the server, returns nil as the error says, get Hit.Position from a LocalScript via a RemoteEvent.

1 Like

You must get Mouse position by invoking a RemoteFunction.

1 Like

It depends on the task, if the server requests it, it must be RemoteFunction, if the player is the one who says the task, it must provide Hit.Position as an argument for RemoteEvent.

2 Likes

Verify if the Mouse.Hit.p is nil and if it’s nil you do not execute the tween service.

1 Like

local mouse = player:GetMouse()

The problem is that it is already a local script ran by a remote.

Am I understanding this correctly:

You are trying to send the Mouse object through a remote and trying to access the property Hit from that object. If this is the case then from what I recall, you can’t (I could be wrong)

You could instead just send over that property itself to the server. But you should be aware that exploiters could very easily manipulate this, if possible try validate it as much as you can on the server’s side.

Also make sure it’s not nil before trying to use it!

You can’t pass the mouse object through a RemoteEvent, you can pass its Hit property though.

Don’t use remote functions please, it will especially be vulnerable to exploiters especially if your Invoking the client on server. As wiki said, the script and localscript both will error if an error occured. Exploiters will use this to damage your server script. Use remoteevents please.

The only time it is vulnerable is is if the server is invoking the client. If the client is an exploiter they can add a wait(100000) at the top to make it yeild forever ont he server. As remote functions yeild until response is given. But the client using a remote function and invoking the server is safe as the client yeild for the response and not the server. So if an exploiter adds a wait(100000) to the serverinvoke script, it’ll only ruin the exploiter’s experience and not everyone elses.

1 Like

That’s what @OP is doing, he’s Invoking client on server, not only that he can yield for a long time or infinitely but he can error for the whole script to break

Ohh ok, I misinterpreted what u said. I thought u said invoking server on client. Sorry abt that

1 Like

InvokeClient() should be wrapped inside pcall() to avoid this.

Or even better, just use spawn or task.spawn on it.

Make sure you send the position of the mouse, not the mouse.

RemoteEvent:FireServer(Mouse.Hit.p);

And receive it as the position from the server.

RemoteEvent.OnServerEvent:Connect(function(player, mousePos)
    TweenService:Create(Sphere, TweenInfos, {Position = mousePos}):Play();
end);
3 Likes

I wouldn’t say that’s even better, because with pcall() you can do something w/ the error object.

1 Like

Worked, thank you so much! I also learnt that you can send data from client to server now. Cheers.

1 Like