Hi, so I want to spawn a dummy/npc where the players’ mouse is pointing at when they are holding the tool. But, the issue is is that the script gets the mouse but returns attempt to index nil with hit when I try to get the position of the mouse.
Here is my current code:
--// Local Script inside tool \\
local dummy = workspace.Dummy
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
print("established locals")
script.Parent.Activated:Connect(function()
print("made func")
mouse.Button1Down:Connect(function()
print("recognized click")
local new = dummy:Clone()
print("cloned")
game.ReplicatedStorage.Spawner:FireServer(plr, mouse, new)
end)
end)
--// Server script in ServerScriptService \\
game.ReplicatedStorage.Spawner.OnServerEvent:Connect(function(plr, oldmouse, new)
local mouse = plr:GetMouse()
new.CFrame = CFrame.new(mouse.Hit.X,mouse.Hit.Y,mouse.Hit.Z)
--The error is on this line ^^^
end)
--// Local Script inside tool \\
local dummy = workspace.Dummy
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
print("established locals")
script.Parent.Activated:Connect(function()
print("made func")
mouse.Button1Down:Connect(function()
print("recognized click")
local new = dummy:Clone()
print("cloned")
game.ReplicatedStorage.Spawner:FireServer(mouse, new) -- removed plr here
end)
end)
--// Server script in ServerScriptService \\
game.ReplicatedStorage.Spawner.OnServerEvent:Connect(function(plr, oldmouse, new)
local mouse = plr:GetMouse()
new.CFrame = CFrame.new(mouse.Hit.X,mouse.Hit.Y,mouse.Hit.Z)
--The error is on this line ^^^
end)
when you fire a remote event to server, the first argument in OnServerEvent will be player by default, rest fired arguments will be next
--// Local Script inside tool \\
local dummy = workspace.Dummy
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
print("established locals")
script.Parent.Activated:Connect(function()
print("made func")
mouse.Button1Down:Connect(function()
print("recognized click")
local new = dummy:Clone()
print("cloned")
game.ReplicatedStorage.Spawner:FireServer(mouse.Hit, new) -- removed plr here
end)
end)
--// Server script in ServerScriptService \\
game.ReplicatedStorage.Spawner.OnServerEvent:Connect(function(plr, oldmouse, new)
local mouse = plr:GetMouse()
new.CFrame = mouse
--The error is on this line ^^^
end)
after reading the code a bit more, there’s more errors.
He’s also sending over an instance that was created on the client. The dummy should be cloned on the server instead.
--// Local Script inside tool \\
local dummy = workspace.Dummy
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
print("established locals")
script.Parent.Activated:Connect(function()
print("made func")
mouse.Button1Down:Connect(function()
print("recognized click")
game.ReplicatedStorage.Spawner:FireServer(mouse.Hit) -- removed plr here
end)
end)
--// Server script in ServerScriptService \\
local dummy = workspace.Dummy
game.ReplicatedStorage.Spawner.OnServerEvent:Connect(function(plr, oldmouse)
local new = dummy:Clone()
print("cloned")
new.CFrame = oldmouse
--The error is on this line ^^^
end)
--// Local Script inside tool \\
local dummy = workspace.Dummy
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
print("established locals")
script.Parent.Activated:Connect(function()
print("made func")
mouse.Button1Down:Connect(function()
print("recognized click")
game.ReplicatedStorage.Spawner:FireServer(mouse.Hit) -- removed plr here
end)
end)
--// Server script in ServerScriptService \\
local dummy = workspace.Dummy
game.ReplicatedStorage.Spawner.OnServerEvent:Connect(function(plr, oldmouse)
local new = dummy:Clone()
print("cloned")
new.CFrame = oldmouse
--The error is on this line ^^^
end)