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

attempt to index global 'chara' (a nil value)?

Asked by 6 years ago
Edited 6 years ago
local Tool = script.Parent
play = game.Players.LocalPlayer
local mouse = play:GetMouse()
chara = play.Character
hum = chara.Humanoid 
root = chara.HumanoidRootPart

local en = true
Tool.Activated:connect(function()
 if not en then return end
 en = false
 Tool.RemoteEvent:FireServer(mouse.Hit)
 wait(0.5)
 en = true
end)

local Tool = script.Parent
play = Tool.Parent.Parent 
chara = play.Character
hum = chara.Humanoid 
root = chara.HumanoidRootPart

local en = true


Tool.RemoteEvent.OnServerEvent:connect(function(play,mousehit)
 local a = hum:LoadAnimation(Tool.Throw)
 a:Play()
 root.CFrame = CFrame.new(root.Position,root.Position + Vector3.new(mousehit.lookVector.x,0,mousehit.lookVector.z))
 wait(0.7)
 local fireball = Tool.Handle:clone()
 fireball.CFrame = Tool.Handle.CFrame
 local bv = Instance.new("BodyVelocity")
 bv.MaxForce = Vector3.new(1e8,1e8,1e8)
 bv.Velocity = mousehit.lookVector * 50
 bv.Parent = fireball
 fireball.CanCollide = false
 fireball.Parent = game.Workspace
 game.Debris:AddItem(fireball,4)
 local ten = true
 fireball.Touched:connect(function(hit)
 if not ten then return end
 ten = false
 local ehum = hit.Parent:findFirstChild("Humanoid") or hit.Parent.Parent:findFirstChild("Humanoid")
 if ehum and ehum ~= hum then
  ehum:TakeDamage(10)
 elseif hit.Anchored  == true and hit.CanCollide == true then
  for i=1,10 do
   local part = fireball:clone()
   part.Size = Vector3.new(0.5,0.5,0.5)
   part.CFrame = fireball.CFrame
   part.ParticleEmitter.LockedToPart = false
   part.BodyVelocity.Velocity = Vector3.new(math.random(-30,30),math.random(-30,30),math.random(-30,30))
   part.Parent = game.Workspace
   game.Debris:AddItem(part,1)
  end
 end
 wait()
 ten = true
 end)
end)

so this is a script that i used a long time ago but it said attemp to index global chara (a nil value) help anyone?! im making a fireball bwt

0
The character doesn't get added the second the script is ran. Try using "play.CharacterAdded:Wait()" ee0w 458 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago

A LocalScript is not really reliable. It executes whether the player's Character has been added or not. To fix that, just add:

repeat 

    wait()

until play.Character

after local mouse = play:GetMouse(). The block of code does just what it says, it repeats wait() until play's Character has been added.

Anyhow, hope this answer helped. Have a great day ahead!

Ad

Answer this question