Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Magic Wand Server to Client Event Malfunctioning?

I am currently in the process of making an HP Wand. Unfortunately, it won't allow me to access the player mouse from the server.

Client

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local event = ReplicatedStorage:WaitForChild("AvadaKedavraEvent")
 
event:FireServer()

Server

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local event = ReplicatedStorage:WaitForChild("AvadaKedavraEvent")

local function avadakedavra()	
	local wand = game.StarterPack.Wand
	local plr = game.Players.LocalPlayer
	local mouse = game.Players.LocalPlayer:GetMouse()
	mouse.Button1Down:connect(function()
		if _G.spell == 1 then
			_G.spell = 0
			local ray = Ray.new(
    		plr.Character.Head.CFrame.p,                           
    		(mouse.Hit.p - plr.Character.Head.CFrame.p).unit * 200) 
			local ignore = game.Players.LocalPlayer.Character
 			local part, position = workspace:FindPartOnRay(ray, plr.Character, false, true)
			local hit, position, normal = workspace:FindPartOnRay(ray, ignore)
			local beam = Instance.new("Part", workspace)
			beam.BrickColor = BrickColor.new("Lime green")
			beam.FormFactor = "Custom"
			beam.Material = "Neon"
			beam.Transparency = 0.5
			beam.Anchored = true
			beam.Locked = true
			beam.CanCollide = false
			beam.Orientation = script.Parent.Handle.Orientation
			beam.Shape = Enum.PartType.Block
			beam.Size = Vector3.new(1,1,1)
			local distance = (plr.Character.Head.CFrame.p - position).magnitude
			print(distance)
			beam.CFrame = CFrame.new(plr.Character.Head.CFrame.p,position)
			local start = plr.Character.Head.CFrame
			for i = 1, 10 do
				beam.CFrame = start:lerp(CFrame.new(position),i/10)
				wait()
			end
			if part then
				local humanoid = part.Parent:FindFirstChild("Humanoid")
 
				if not humanoid then
					humanoid = part.Parent.Parent:FindFirstChild("Humanoid")
				end
 
				if humanoid then
					humanoid.Health = humanoid.Health - 110
				end
				
				beam:Destroy()
			end
		end
	end)
end

	
event.OnServerEvent:Connect(avadakedavra)

ServerScriptService.Script:7: attempt to index field 'LocalPlayer' (a nil value)

4 comments

0

You may NOT use the mouse on the server

0

hol' up im'a right an answer

0

You can't call LocalPlayer in a regular script because the server has no local player.

0

^ yes

1 answer

Client

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local event = ReplicatedStorage:WaitForChild("AvadaKedavraEvent")

local mouse = game.Players.LocalPlayer:GetMouse()

mouse.Button1Down:Connect(function()
--	mouse.Button1Up:Wait() -- Uncomment if you want a full click
	event:FireServer(mouse.Hit)
end)

Server

-- Sorry for the formatting :(
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local event = ReplicatedStorage:WaitForChild("AvadaKedavraEvent")

local function avadakedavra(plr,hit)   
    local wand = game.StarterPack.Wand
        if _G.spell == 1 then
            _G.spell = 0
            local ray = Ray.new(
            plr.Character.Head.CFrame.p,                           
            (hit.p - plr.Character.Head.CFrame.p).unit * 200) 
            local ignore = game.Players.LocalPlayer.Character
            local part, position = workspace:FindPartOnRay(ray, plr.Character, false, true)
            local hit, position, normal = workspace:FindPartOnRay(ray, ignore)
            local beam = Instance.new("Part", workspace)
            beam.BrickColor = BrickColor.new("Lime green")
            beam.FormFactor = "Custom"
            beam.Material = "Neon"
            beam.Transparency = 0.5
            beam.Anchored = true
            beam.Locked = true
            beam.CanCollide = false
            beam.Orientation = script.Parent.Handle.Orientation
            beam.Shape = Enum.PartType.Block
            beam.Size = Vector3.new(1,1,1)
            local distance = (plr.Character.Head.CFrame.p - position).magnitude
            print(distance)
            beam.CFrame = CFrame.new(plr.Character.Head.CFrame.p,position)
            local start = plr.Character.Head.CFrame
            for i = 1, 10 do
                beam.CFrame = start:lerp(CFrame.new(position),i/10)
                wait()
            end
            if part then
                local humanoid = part.Parent:FindFirstChild("Humanoid")

                if not humanoid then
                    humanoid = part.Parent.Parent:FindFirstChild("Humanoid")
                end

                if humanoid then
                    humanoid.Health = humanoid.Health - 110
                end

                beam:Destroy()
	end
	end
end


event.OnServerEvent:Connect(avadakedavra)

This should work! Happy scripting!

1 comment

0

It isn't working.

This discussion is preserved as it was. New answers, comments, and votes are closed.