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

Is it possible to use Mouse.Hit with server scripts if not how can I fix this?

Asked by
Cikeruw 14
2 years ago

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)

1 answer

Log in to vote
0
Answered by 2 years ago

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)
0
Im still confused what code will i use to use mouse.hit Cikeruw 14 — 2y
Ad

Answer this question