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

I need my gun to be a server script but i need local player at the same time. Help?

It is ok if its in a different script

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local k = Instance.new("Part")
local Tool = script.Parent.Parent
local canshoot = false

Tool.Equipped:Connect(function()
    canshoot = true
end)
Tool.Unequipped:Connect(function()
    canshoot = false
end)

Mouse.Button1Down:Connect(function()
    if canshoot == true then
        wait(0.5)
        k.Parent = game.Workspace
        k.Color = Color3.fromRGB(1,1,1)
        k.Size = Vector3.new(0.08, 0.08, 0.08)
        k.Anchored = true
        k.CanCollide = false
        k.Position = Vector3.new(Mouse.Hit)
        k.CFrame = Mouse.Hit
        script.Parent.Shoot:Play()
        repeat  wait(.001) script.Parent.PointLight.Enabled = true until script.Parent.Shoot.Playing == false       
        if script.Parent.Shoot.Playing == false then
            script.Parent.PointLight.Enabled = false
        end
    end
end)

function onTouched(hit)
        local human = hit.Parent:findFirstChild("Humanoid")
        if (human ~= nil) then
                human.Health = human.Health - 7
if k.Touched then
	k:Destroy()
end
        end
end
k.Touched:connect(onTouched)

1 comment

0

The below answer is rubbish, just dedicate a script to do the server stuff, and dedicate a local script to doing the client stuff, and use a either a RemoteEvent or a RemoteFunction to communicate between the server and the client.

1 answer

remplace the :

local Player  = game.Players.LocalPlayer

with :

game.Players.PlayerAdded:Connect(function(player))
-- insert your code here and remplace Player with player
end)

--Edited The New Code:

game.Players.PlayerAdded:Connect(function(player)
	local Mouse = player:GetMouse()
	local k = Instance.new("Part")
	local Tool = script.Parent.Parent
	local canshoot = false

	Tool.Equipped:Connect(function()
	    canshoot = true
	end)
	Tool.Unequipped:Connect(function()
	    canshoot = false
	end)
	
	Mouse.Button1Down:Connect(function()
	    if canshoot == true then
	        wait(0.5)
	        k.Parent = game.Workspace
	        k.Color = Color3.fromRGB(1,1,1)
	        k.Size = Vector3.new(0.08, 0.08, 0.08)
	        k.Anchored = true
	        k.CanCollide = false
	        k.Position = Vector3.new(Mouse.Hit)
	        k.CFrame = Mouse.Hit
	        script.Parent.Shoot:Play()
	        repeat  wait(.001) script.Parent.PointLight.Enabled = true until script.Parent.Shoot.Playing == false       
	        if script.Parent.Shoot.Playing == false then
	            script.Parent.PointLight.Enabled = false
	        end
	    end
	end)
	
	function onTouched(hit)
	        local human = hit.Parent:findFirstChild("Humanoid")
	        if (human ~= nil) then
	                human.Health = human.Health - 7
	if k.Touched then
	    k:Destroy()
	end
	        end
	end
	k.Touched:connect(onTouched)
end)

1 comment

0

looking back at my old posts, this is garbage what you tried making me do. nothing explained

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