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

Gun script causes bullet to only go in one direction?

I am trying to make a script that launches a colored block from a held tool in the direction you click your mouse, but I can't get it to work. It does not work in server mode, and in studio mode it causes the bullet to always fly in the same direction, no matter where you point your mouse or how you turn your body.

In a normal script I have

local tool = script.Parent
local bullet = game.ReplicatedStorage.bullets.needle 
local sound = tool.Sound


function pew()
sound:Play()
local arrow = bullet:clone() 
arrow.Parent = tool 
game:GetService("Debris"):AddItem(arrow, .22)
	local v = Instance.new ("BodyVelocity", arrow)
v.velocity = arrow.CFrame.lookVector *90
v.maxForce = Vector3.new(math.huge, math.huge, math.huge)
tool.ManualActivationOnly = true
wait (.3)
tool.ManualActivationOnly = false
end

tool.Activated:connect(pew)

This script works just fine in studio mode, but spawns the bullet in the center of the map in server mode.

In a local script I have


local tool = script.Parent
local mouse = game.Players.LocalPlayer:GetMouse()

function pew()
	wait (.001)
local arrow = script.Parent.needle
	arrow.CFrame = CFrame.new(tool.Handle.Position, mouse.Hit.p)

end

tool.Activated:connect(pew)

This is the script to give the spawned bullet it's direction. It does not work in server mode, and only changes which way the bullet faces in studio mode. Almost what I want, but without the momentum in the correct direction it's useless.

I know there is a thing you can turn off that might help with making it work better in server mode, but I don't know what it's called or how to find it. I am not really sure what to do about the bullet shooty direction though.

What can I do to fix this?

1 comment

0

It might be a loading issue, try adding a waitforchild or a "nil" checker

1 answer

You set your clone bullet's direction and speed, but you never specify it's position (so it probably inherits the position of the original bullet).

Also, why do you change it's direction in a localscript? That might explain why it doesn't work in multiplayer.

Hope that helps. Please upvote and accept my answer if it did. :) If you need anything else, just message me or comment on my answer.

1 comment

0

I change it's direction in a local script so it shoots towards the mouse of the player holding the tool, because I don't know how to get that without using the localplayer function. I'm not quite sure how to set it's position relative to the player either.

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