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

attempt to index local 'plr' (a nil value)?

Due to the fact that i cannot run the OnServerEvent with local script how can i make it so fireball fires in direction of mouse?

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Fire = Instance.new("RemoteEvent", ReplicatedStorage)
Fire.Name = "FireBall"
local plr = game:GetService([[Players]]).LocalPlayer
local mouse = plr:GetMouse()
local Char = plr.Character or plr.CharacterAdded:wait()
local lefthand = Char.LeftHand
 
Fire.OnServerEvent:connect(function(Player, Debounce)
	local Char = Player.Character or Player.CharacterAdded:wait()
	Debounce = true
	local Fireball = Instance.new("Part")
	Fireball.Name = "FireBall"
	Fireball.Shape =  Enum.PartType.Ball
	Fireball.Size  = Vector3.new(3,3,3)
    Fireball.Material = Enum.Material.Neon
    Fireball.CanCollide = false
    Fireball.Parent = Char
    Fireball.CFrame = Char.HumanoidRootPart.CFrame*CFrame.new(2,0,-6)
    Fireball.BrickColor = BrickColor.new("Bright orange")


 local BV = Instance.new([[BodyVelocity]], Fireball)
BV.Velocity = Vector3.new(mouse.Hit.lookVector * 90)
BV.MaxForce = Vector3.new(lefthand.CFrame.lookVector, 5, 0)

--oof
    local dmg = script.Dmg:Clone()
    dmg.Parent = Fireball
    dmg.Disabled = false
    wait(0.5)
    dmg:Destroy()
    wait(4)
    Debounce = false
    Fireball:Destroy()

	
end)

6 comments

0

You're using OnServerEvent in a localscript?

0

You are either using OnServerEvent in a local script, or you are trying to access the local player from a server script. You can not access the local player from a server script.

0

Can i fix that somehow then?

0

What i'm trying to ask is how can i make fireball fire in direction of mouse, But for script to still be onserverevent?

0

Why are you using OnServerEvent if you're never using FireServer?

0

I set it on Local Player i made it so when i press e it fires event

2 answers

###As mentioned by others, you cannot use LocalPlayer, Mouse from a Script. Since you need the Hit of the mouse, simply send it as a parameter.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Fire = Instance.new("RemoteEvent") -- parent argument deprecated 
Fire.Name = "FireBall"
Fire.Parent = ReplicatedStorage -- parent LAST

Fire.OnServerEvent:Connect(function(Player, Hit, Debounce) -- connect is deprecated, use Connect
    -- Hit will be the mouse Hit 
    local Char = Player.Character or Player.CharacterAdded:Wait() -- :wait() is deprecated 
    local lefthand = Char.LeftHand
    Debounce = true
    local Fireball = Instance.new("Part")
    Fireball.Name = "FireBall"
    Fireball.Shape =  Enum.PartType.Ball
    Fireball.Size  = Vector3.new(3,3,3)
    Fireball.Material = Enum.Material.Neon
    Fireball.CanCollide = false
    Fireball.CFrame = Char:GetPrimaryPartCFrame()*CFrame.new(2,0,-6)
    Fireball.BrickColor = BrickColor.new("Bright orange")
    Fireball.Parent = Char

    local BV = Instance.new("BodyVelocity")
    BV.Velocity = Hit.LookVector*90 -- LookVector is already a Vector3
    BV.MaxForce = Vector3.new(lefthand.CFrame.LookVector.X, 5, 0)
    -- LookVector is the vector, the X property is the X coordinates of the vector 
    BV.Parent = Fireball

    local dmg = script.Dmg:Clone()
    dmg.Parent = Fireball
    dmg.Disabled = false
    wait(0.5)
    dmg:Destroy()
    wait(4)
    Debounce = false
    Fireball:Destroy()


end)

-- somewhere in a localscript
Fire:FireServer(mouse.Hit, Debounce)
-- Do NOT pass the player! 

###It is also worth noting that your strings were inconsistent. In some lines you would use "" and in others you would use [[]]. Though it doesn't affect the code, it is important to be consistent. Your code also had a few deprecated items, which should not be used. Here is a little article on deprecation and what it can do to your code in the future if it contains deprecated items.

What's happening here is that you seem to be using LocalPlayer which is only accessible to the client. A possible fix would be to run a code in a local script telling the server to create a fireball.

1 comment

0

how could i connect damage to it?

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