Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by
iuclds 720 Moderation Voter
4 years ago

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)
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. Unhumanly 152 — 4y

1 answer

Log in to vote
-2
Answered by 4 years ago
Edited 4 years ago

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)
0
looking back at my old posts, this is garbage what you tried making me do. nothing explained iuclds 720 — 4y
Ad

Answer this question