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

Gun only working in singleplayer?

--LocalScript
local player 
local weapon = script.Parent
local mForRest
local bullet = weapon.hitter
local Var = weapon.Target

function shoot()
	local target = mForRest.Hit
	print(target)
	Var.Value = target
	
end
weapon.Equipped:connect(function(mouse)
	player = game:GetService("Players").LocalPlayer
	mForRest = mouse
	mouse.Button1Down:connect(shoot)
end)

-- Script
local target = script.Parent.Target
local bullet = script.Parent.hitter

target.Changed:connect(function(value)
	print("ShotsFired")
	local shot = bullet:Clone()
	shot.Parent = workspace
	shot.CFrame = value
	shot.Size = Vector3.new(0.2,0.2,0.2)
end)

Both scripts are located within the tool. Hitter is an instance of a part and Target is a CFrame variable to communicate between the scripts.

Anyone know why this only works in single player?

Error log

hitter is not a valid member of Tool

1 answer

Use WaitForChild

You have to wait for things to load because sometimes the script loads before the objects. Example,

--LocalScript
local player 
local weapon = script.Parent
local mForRest
local bullet = weapon:WaitForChild("hitter")
local Var = weapon:WaitForChild("Target")

function shoot()
    local target = mForRest.Hit
    print(target)
    Var.Value = target

end
weapon.Equipped:connect(function(mouse)
    player = game:GetService("Players").LocalPlayer
    mForRest = mouse
    mouse.Button1Down:connect(shoot)
end)


-------------------------------------------------------

-- Script
local target = script.Parent:WaitForChild("Target")
local bullet = script.Parent:WaitForChild("hitter")

target.Changed:connect(function(value)
    print("ShotsFired")
    local shot = bullet:Clone()
    shot.Parent = workspace
    shot.CFrame = value
    shot.Size = Vector3.new(0.2,0.2,0.2)
end)

Let me know if that works for you.

Good Luck!

If I helped, please don't forget to accept my answer.

4 comments

0

It Worked! :D Thanks :)

0

New on the site, how to accept an answer

0

You should see something like this, https://gyazo.com/4396906f650430c6562874073042e53c

0

If you don't, log out and in again. If you still don't see it, report the bug to the feedback to the left.

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