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

gun script works in studio, but not in player?

wait(1)
d = true
script.Parent.Equipped:connect(function(mouse)
	mouse.Button1Down:connect(function()
		if d == true then
			d = false
			print'fgf'
		local targetPos = game.Players.LocalPlayer:WaitForChild("Character").Humanoid.TargetPoint
		local lookAt = (targetPos - game.Players.LocalPlayer:WaitForChild("Character").Head.Position).unit
		print'wwwwwwwwwwwwwwwwwwww'
		local p = Instance.new("Part")
		p.Position = script.Parent.Handle.Position
		p.Parent = game.Workspace
		p.CanCollide = true
		p.Name = script.Parent.Parent.Name
		p.Size = Vector3.new(.4,.4,.4)
		p.BrickColor = BrickColor.new("New Yeller")
		p.Velocity = lookAt * 300
		local Bforce = Instance.new("BodyForce", workspace)
    	Bforce.Parent = p
    	Bforce.force = Vector3.new(0, 196.2, 0) * p:GetMass()
		local s = game.ReplicatedStorage.hit:Clone()
		s.Parent = p
		s.Disabled = false
		game:GetService("Debris"):AddItem(p, 5)
		wait(.5)
		d = true
		end
	end)
end)

this doesnt work in studio, and if it isnt working in studio, im just guessing it wont work in player. hit is the damage script for this gun, im not going to show it because there is no need to, because this script only prints fgf. if i take out the waitforchilds, it works in studio, but still not in player.

2 answers

✓ Accepted answer

You should use a repeat when waiting for player.Character.

wait(1)
d = true
script.Parent.Equipped:connect(function(mouse)
    mouse.Button1Down:connect(function()
        if d == true then
            d = false
            print'fgf'
		repeat wait() until player.Character
        local targetPos = game.Players.LocalPlayer.Character.Humanoid.TargetPoint
        local lookAt = (targetPos - game.Players.LocalPlayer.Character.Head.Position).unit
        print'wwwwwwwwwwwwwwwwwwww'
        local p = Instance.new("Part")
        p.Position = script.Parent.Handle.Position
        p.Parent = game.Workspace
        p.CanCollide = true
        p.Name = script.Parent.Parent.Name
        p.Size = Vector3.new(.4,.4,.4)
        p.BrickColor = BrickColor.new("New Yeller")
        p.Velocity = lookAt * 300
        local Bforce = Instance.new("BodyForce", workspace)
        Bforce.Parent = p
        Bforce.force = Vector3.new(0, 196.2, 0) * p:GetMass()
        local s = game.ReplicatedStorage.hit:Clone()
        s.Parent = p
        s.Disabled = false
        game:GetService("Debris"):AddItem(p, 5)
        wait(.5)
        d = true
        end
    end)
end)

Player.Character:WaitForChild("Humanoid")

Since the character does not load before some seconds... therefore it will be nil

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