Hello, so i made this script that generates a lightning bolt that follows a random path out of the player (https://imgur.com/a/eknhlJL , made this with the help of some free models).
Using this concept I am trying to make a wand that casts some kind of electricity spell. I want this spell to have the same kind of trail but i keep running into this issue where the variable won't change.
this is the part of the wand script that causes the problem: (NOT the full script)
local ball = Instance.new("Part", JunkStore)
ball.Name = "ball"
ball.Size = Vector3.new(0.2,0.2,0.2)
ball.Anchored = false
ball.CanCollide = false
ball.CFrame = wandtip.CFrame * CFrame.new(0,0,-0.75)
local BV = Instance.new("BodyVelocity",ball)
BV.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
BV.Velocity = mousehit.lookVector*10
wait(0.1)
PreviousB = ball.CFrame.Position
Run.Heartbeat:Connect(function()
print(PreviousB,"WAND")
On line 16 it always prints out the same number,
however if i try this on the other script that generates the trail on the player:
Object = Player.Character.HumanoidRootPart
Run = game["Run Service"]
PreviousP = Object.CFrame.p
Player.Character.Humanoid.WalkSpeed = 100
Run.Heartbeat:Connect(function()
print(PreviousP)
here it always prints out a different number
I really don't understand why the variable changes with the player's script but not with the wand's script.
If any of this is unclear just comment and i'll try to explain it further.
Thanks for trying to help!
FULL SCRIPTS:
Wand:
wait(0.5)
wand = script.Parent
wandtip = wand.WandTip
player = script.Parent.Parent.Parent
playername = player.Name
char = workspace:WaitForChild(playername,5)
Humanoid = char:WaitForChild("Humanoid")
MouseSyncEvent = game.ReplicatedStorage.WandMouseSyncToScript
WandUI = player.PlayerGui.WandUI
WandUIScript = WandUI.MainUIScript
saf = Humanoid:LoadAnimation(wand.Stuffs["Swish And Flick"])
animDB = false
Run = game["Run Service"]
rate_per_second = 2
SpellVal = 0
--
Spell1 = false
Spell2 = false
Spell3 = false
--
JunkStore = Instance.new("Model",workspace)
script.Parent.Equipped:Connect(function()
WandUI.Enabled = true
WandUIScript.Disabled = false
end)
script.Parent.Unequipped:Connect(function()
WandUI.Enabled = false
WandUIScript.Disabled = true
end)
game.ReplicatedStorage.ValueMoverWand.OnServerEvent:Connect(function(p,SVall)
SpellVal = SVall
end)
script.Parent.Activated:Connect(function()
if animDB == false then
animDB = true
saf:Play()
MouseSyncEvent.OnServerEvent:Connect(function(p,mh) -- probs not efficient but will do
mousehit = mh
end)
--Spellcasting--
if SpellVal == 0 then -- No magic / False cast
wandtip.Sparks1.Enabled = true
wandtip.Sparks2.Enabled = true
wait(0.5)
wandtip.Sparks1.Enabled = false
wandtip.Sparks2.Enabled = false
end
if SpellVal == 1 then -- Electricity
local children = JunkStore:GetChildren()-- delete old electricity bolt
for i=1,#children do
if children[i].Name == "ball" then
children[i]:Destroy()
end
end
--
local ball = Instance.new("Part", JunkStore)
ball.Name = "ball"
ball.Size = Vector3.new(0.2,0.2,0.2)
ball.Anchored = false
ball.CanCollide = false
ball.CFrame = wandtip.CFrame * CFrame.new(0,0,-0.75)
local BV = Instance.new("BodyVelocity",ball)
BV.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
BV.Velocity = mousehit.lookVector*10
wait(0.1)
PreviousB = ball.CFrame.Position
Run.Heartbeat:Connect(function()
print(PreviousB,"WAND")
if (ball.CFrame.p - PreviousB).magnitude > 1 then
local distance = (ball.CFrame.Position - PreviousB).magnitude
---
local Path = Instance.new("Part",ball)
Path.Anchored = true
Path.CanCollide = false
Path.Size = Vector3.new(0.1,0.1,distance)
Path.CFrame = CFrame.new(ball.CFrame.Position, PreviousB) * CFrame.new(0,0,-distance/2)
local Point1 = Instance.new("Part",ball)
Point1.Anchored = true
Point1.CanCollide = false
Point1.Size = Vector3.new(0.2,0.2,0.2)
Point1.CFrame = Path.CFrame * CFrame.new(0,0,-Path.Size.Z/2)
* CFrame.new(Path.Size.X/2 ,0 ,0)
* CFrame.new(math.random(-1,1),math.random(-1,1),0)
end
end)
end
if SpellVal == 2 then -- Bombarda
end
----------------
wait(0.5)
animDB = false
end
end)
Player lightning bolt:
wait(0.5)
Player = game.Players.LocalPlayer
Object = Player.Character.HumanoidRootPart
Arm1 = Player.Character.RightUpperArm
PreviousA1 = Arm1.CFrame.p
LightMaxX = 1
LightMinX = 0
LightMaxY = 1
LightMinY = 0
Color = BrickColor.Yellow()
PColor = Color3.new(255,255,0)
orbFX = game.Lighting.OrbFX
FX = orbFX.TrailBoltsFLASH
PrevPoint1 = nil
PrevPoint2 = nil
Run = game["Run Service"]
PreviousP = Object.CFrame.p
Player.Character.Humanoid.WalkSpeed = 100
Run.Heartbeat:Connect(function()
print(PreviousP)
if (Object.CFrame.p - PreviousP).magnitude > 5 and Player.Character.Humanoid.WalkSpeed > 25 then
local Model = workspace.Ray
local distance = (Object.CFrame.p - PreviousP).magnitude
---------
local Light = Instance.new("Part",Model)
Light.Anchored = true
Light.CanCollide = false
Light.Transparency = 1
Light.BrickColor = BrickColor.Black()
Light.Size = Vector3.new(0.1,0.1,distance)
Light.CFrame = CFrame.new(Object.CFrame.p , PreviousP) * CFrame.new(0,0,-distance/2)
local Point1 = Instance.new("Part",Model)
Point1.Transparency = 1
Point1.Anchored = true
Point1.CanCollide = false
Point1.Size = Vector3.new(1,1,1)
Point1.CFrame = Light.CFrame * CFrame.new(0,0,-Light.Size.Z/2)
* CFrame.new(Light.Size.X/2 ,0 ,0)
* CFrame.new(math.random(LightMinX,LightMaxX),0,0)
* CFrame.new(0,math.random(LightMinY,LightMaxY),0)
local Point2 = Instance.new("Part",Model)
Point2.Transparency = 1
Point2.Anchored = true
Point2.CanCollide = false
Point2.Size = Vector3.new(1,1,1)
Point2.CFrame = Light.CFrame * CFrame.new(0,0,Light.Size.Z/2)
* CFrame.new(Light.Size.X/2 ,0 ,0)
* CFrame.new(math.random(LightMinX,LightMaxX),0,0)
* CFrame.new(0,math.random(LightMinY,LightMaxY),0)
if PrevPoint2 == nil or PrevPoint2 == "Empty" then
local distance = (Point2.CFrame.p - Point1.CFrame.p).magnitude
local bolts2 = FX:Clone()
local Light = Instance.new("Part",Model)
bolts2.Parent = Light
Light.Anchored = true
Light.CanCollide = false
Light.Transparency = 0
Light.Size = Vector3.new(0.1,0.1,distance)
Light.Material = "Neon"
Light.BrickColor = Color
Light.CFrame = CFrame.new(Point2.CFrame.p , Point1.CFrame.p) * CFrame.new(0,0,-distance/2)
elseif PrevPoint2 ~= nil then
local distance = (Point2.CFrame.p - PrevPoint2).magnitude
local bolts2 = FX:Clone()
local Light = Instance.new("Part",Model)
bolts2.Parent = Light
Light.Anchored = true
Light.CanCollide = false
Light.Transparency = 0
Light.Size = Vector3.new(0.1,0.1,distance)
Light.Material = "Neon"
Light.BrickColor = Color
Light.CFrame = CFrame.new(Point2.CFrame.p , PrevPoint2) * CFrame.new(0,0,-distance/2)
end
PreviousP = Object.CFrame.p
PreviousA1 = Arm1.CFrame.p
PrevPoint1 = Point1.CFrame.p
PrevPoint2 = Point2.CFrame.p
local Children = Model:GetChildren()
for i = 1,20 do
local Remove = coroutine.wrap(function()
for i = 1,#Children do
if Children[i]:IsA("Part") then
Children[i].Transparency = Children[i].Transparency + 0.07 --change transparency for length of the streak
if Children[i].Transparency >= 1 and Children[i] ~= Light and Children[i] ~= Point1 and Children[i] ~= Point2 then
game.Debris:AddItem(Children[i],0)
end
end
if Children[i].ClassName ~= "Part" then
game.Debris:AddItem(Children[i],0)
end
end
end)
Remove()
wait(0.0001)
end
game.Debris:AddItem(Light,0)
game.Debris:AddItem(Point1,0)
game.Debris:AddItem(Point2,0)
end
end)

4 comments
You only update PreviousB when the tool is activated in the wand script, while you constantly update PreviousP in the bolt script using the .Heartbeat function. That's why you keep getting the same number in your wand script.
Is there a simple way to get around this ?
You can just put line 87 into the .Heartbeat function so it can constantly update.
Saddly this doesn't work doing this makes the PreviousB identically the same as ball.CFrame.p this means that (ball.CFrame.p - PreviousB).magnitude = 0 so it doesn't get past line 92