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

Making a "magic wand"; why won't this work correctly?

Hey guys, excuse my ignorance in tool making. This is probably the first tool I've ever made on Roblox. I'm trying to make a wand that executes "magic" like bullets. It works similar to a gun.

The tool works perfectly when I'm standing at 0,0,0. When I move anywhere else, the wand makes "bullets" but they are static and don't behave like they should.

Here's the code:

plr = game.Players.LocalPlayer
gn = script.Parent
mouse = plr:GetMouse()

function Fire()
	local bullet = Instance.new("Part", workspace)
	game.Debris:AddItem(bullet, 3)
	bullet.Shape = "Ball"
	bullet.TopSurface = "Smooth"
	bullet.BottomSurface = "Smooth"
	bullet.BrickColor = BrickColor.new("Black")
	bullet.CanCollide = false
	bullet.CFrame = gn.Handle.CFrame
	bullet.CFrame = CFrame.new(bullet.Position,mouse.Hit.p)
	bullet.Anchored = false
	local ps = Instance.new("BodyPosition", bullet)
	ps.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
	ps.Position = Vector3.new(mouse.Hit.p)
	print(mouse.Hit.p)
end
gn.Activated:connect(Fire)

Here's how the wand behaves when I have not moved:

https://gyazo.com/30b9fc7e340a87c477ca49ebaedc9774

Here's how it behaves when I move:

https://gyazo.com/399cce0240b5ae97b39cd95dc247dbc9

Any ideas?

1 answer

Ok, figured it out (kinda).

Setting the CFrame of bullet does something funky with it. Just set the position instead. Also, you are setting the BodyPosition's Position wrong. Mouse.hit.p IS already a Vector3 position, so by doing Vector3.new(already vector3) you will always get 0

Working script:

plr = game.Players.LocalPlayer
gn = script.Parent
mouse = plr:GetMouse()

function Fire()

    local bullet = Instance.new("Part", workspace)
	game.Debris:AddItem(bullet, 3)

    bullet.Shape = "Ball"
    bullet.TopSurface = "Smooth"
    bullet.BottomSurface = "Smooth"
    bullet.BrickColor = BrickColor.new("Black")
    bullet.CanCollide = false
    bullet.Position = gn.Handle.CFrame.p -- set position instead


    local ps = Instance.new("BodyPosition", bullet)
    ps.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
    ps.Position = mouse.Hit.p -- actually sets to position, not (0,0,0)



end
gn.Activated:connect(Fire)

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