Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

ServerScriptService.Script:10: attempt to index nil with 'X'?

Asked by 2 years ago

So Basically I want to clone something where the mouse was clicked here is the code.

local replicated_Storage = game:GetService("ReplicatedStorage")
local event = replicated_Storage.Event

local UserInputService = game:GetService("UserInputService")
local player = game:GetService("Players").LocalPlayer
local mouse = player:GetMouse()

mouse.Button1Down:Connect(function()
    event:FireServer()
end)

Second piece of code: Server side

local replicated_Storage = game:GetService("ReplicatedStorage")
local event = replicated_Storage.Event
local UserInputService = game:GetService("UserInputService")
local black_Hole = replicated_Storage:WaitForChild("BlackHole")

event.OnServerEvent:Connect(function(player)
    local mouse = player:GetMouse()
    local new_Blackhole = black_Hole:Clone()
    new_Blackhole.Parent = game.Workspace
    new_Blackhole.Position = Vector3.new(mouse.X,mouse.Y,0)
    new_Blackhole.Anchored = true
    print(new_Blackhole.Position)
end)
0
maybe try tonumber(mouse.X) Amanda314159 291 — 2y

1 answer

Log in to vote
0
Answered by 2 years ago

Hello!

The property X and property Y of mouse are not replicated to the server.

Additionally, the X and Y position of the mouse describes the mouse's position on the screen, not the physical location within the Workspace.

But this is why Mouse.Hit exists!

Mouse.Hit will give you a CFrame value of the mouse's physical location within the world!

To convert the CFrame in to a Vector3 value like you're trying to use, you can do CFrame.Postion.

Also note that the Mouse.Hit property is client replicated, so you should send the information through the remote event!

I'll try to fix your scripts below:

Local Script:

local replicated_Storage = game:GetService("ReplicatedStorage")
local event = replicated_Storage.Event

local UserInputService = game:GetService("UserInputService")
--You didn't use the UserInputService. Any reason its here?
local player = game:GetService("Players").LocalPlayer
local mouse = player:GetMouse()

mouse.Button1Down:Connect(function()
    event:FireServer(mouse.Hit)
end)

Server Side:

local replicated_Storage = game:GetService("ReplicatedStorage")
local event = replicated_Storage.Event
local UserInputService = game:GetService("UserInputService")
local black_Hole = replicated_Storage:WaitForChild("BlackHole")

event.OnServerEvent:Connect(function(player, Hit)
    local new_Blackhole = black_Hole:Clone()
    new_Blackhole.Parent = game.Workspace
    new_Blackhole.Position = Hit.Position
    new_Blackhole.Anchored = true
    print(new_Blackhole.Position)
end)
0
About the UserInputService I wanted to try something but didn't work so it doesn't really work. But thanks I'll try it out. II_Goodlu 8 — 2y
0
But why is the second param of the OnServerEvent Hit. Why did you add that in. Also sorry I'm very new to remote events just started yesterday. II_Goodlu 8 — 2y
0
I added that as the second tuple because I'm sending that information through the remote event. The server cannot access Mouse.Hit (A CFrame value), so we instead just send the value through the remote even to the server. Makes sense? wwwaylon09 113 — 2y
Ad

Answer this question