I got an error saying Players.cikeruw.Backpack.Tool.Script:16: attempt to index nil with 'Hit'. If you cannot use Mouse.hit with server scripts how will I fix this.
This is my local script.
local Tool = script.Parent local Players = game:GetService("Players") local Player = Players.LocalPlayer local Mouse = Player:GetMouse() Mouse.Button1Up:Connect(function() if Tool.Parent.Name == Player.Character.Name then game.ReplicatedStorage.LightningBolt:FireServer() end end)
This is my server script
local Part = game.ReplicatedStorage.Wedge game.ReplicatedStorage.LightningBolt.OnServerEvent:Connect(function(Player) local Mouse = Player:GetMouse() local Tool = script.Parent if Tool then if Tool.Parent.Name == Player.Character.Name then local partclone = Part:Clone() partclone.CanCollide = false partclone.CFrame = Player.Character.HumanoidRootPart.CFrame local VectorForce = Instance.new("VectorForce") partclone.Parent = game.Workspace VectorForce.Force = Player.Character.HumanoidRootPart.Position + Mouse.Hit.LookVector * 100 partclone.Touched:Connect(function(hit) if hit.Parent:FindFirstChild("Humanoid")then if hit.Parent.Name == Player.Character.Name then wait() hit.Parent:FindFirstChild("Humanoid").Health = hit.Parent:FindFirstChild("Humanoid").Health - 5 * Player.leaderstats.Level.Value wait(5) end end end) end end end)
If you need to use Mouse.Hit on the server, use a RemoteEvent to send it from the client.
Client
local RemoteEvent = YourRemoteEvent local Player = game.Players.LocalPlayer local Mouse = Player:GetMouse() RemoteEvent:FireServer(Mouse.Hit)
Server
local RemoteEvent = YourRemoteEvent RemoteEvent.OnServerEvent:Connect(function(player, mouseCFrame) -- Use the cframe end)