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

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

Asked by 5 years ago
Edited 5 years ago

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)
0
You're using OnServerEvent in a localscript? MythicalShade 420 — 5y
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. valchip 789 — 5y
0
Can i fix that somehow then? Davidxd00 0 — 5y
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? Davidxd00 0 — 5y
View all comments (2 more)
0
Why are you using OnServerEvent if you're never using FireServer? MythicalShade 420 — 5y
0
I set it on Local Player i made it so when i press e it fires event Davidxd00 0 — 5y

2 answers

Log in to vote
0
Answered by 5 years ago

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.

Ad
Log in to vote
-1
Answered by
ffejyos 10
5 years ago

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.

0
how could i connect damage to it? Davidxd00 0 — 5y

Answer this question