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

How can I fix this server script that works only on play solo? [closed]

Asked by 5 years ago

This script fires everytime the weapon is shot. It works perfectly fine in solo mode but while in a local server, it doesn't. What should I do?

local RepStorage = game:GetService("ReplicatedStorage")
local Remote = RepStorage:WaitForChild("Remote")
local LocalRemote = Remote:WaitForChild("GunRaycast")

LocalRemote.OnServerEvent:connect(function(player, BaseDamage, RayLength, Tool, BarrlePos, aim, raycast)

    local function tagHumanoid(humanoid)
    local plr=game.Players:playerFromCharacter(Tool.Parent)
        if plr~=nil then
            local tag=Instance.new("ObjectValue")
            tag.Value=plr
            tag.Name="creator"
            tag.Parent=humanoid
            delay(2,function()
                if tag~=nil then
                    tag.Parent=nil
                end
            end)
        end
    end

    local function computeDirection(vec)
    local lenSquared = vec.magnitude * vec.magnitude
    local invSqrt = 1 / math.sqrt(lenSquared)
    return Vector3.new(vec.x * invSqrt, vec.y * invSqrt, vec.z * invSqrt)
    end

    local Bullet=Instance.new("Part")
    Bullet.Name="Bullet"
    Bullet.BrickColor=BrickColor.new("Bright orange")
    Bullet.Anchored=true
    Bullet.CanCollide=false
    Bullet.Locked=true
    Bullet.Size=Vector3.new(1,1,1)
    Bullet.Transparency=0
    Bullet.Material = "Neon"    
    Bullet.formFactor=0
    Bullet.TopSurface=0
    Bullet.BottomSurface=0
    local mesh=Instance.new("SpecialMesh")
    mesh.Parent=Bullet
    mesh.MeshType="Brick"
    mesh.Name="Mesh"
    mesh.Scale=Vector3.new(1,1,1)

    local bullet = Bullet:Clone()   
    bullet.BrickColor = BrickColor.Black()

    bullet.Name = "Raycast"
    local totalDist=0
    local Lengthdist=-RayLength/.5
    local startpoint=Tool.Handle.CFrame*BarrlePos
    local dir=(aim)-startpoint
    dir=computeDirection(dir)
    local cfrm=CFrame.new(startpoint, dir+startpoint)
    local hit,pos,normal,time=raycast(game.Workspace, startpoint, cfrm*Vector3.new(0,0,Lengthdist)-startpoint, function(brick)
        if brick.Name=="Glass" then
            return true
        elseif brick.Name=="Bullet" or brick.Name=="BulletTexture" then
            return false
        elseif brick:IsDescendantOf(Tool.Parent) then
            return false
        elseif brick.Name=="Handle" then
            if brick.Parent:IsDescendantOf(Tool.Parent) then
                return false
            else
                return true
            end
        end
        return true
    end)

    if hit~=nil then
        local humanoid=hit.Parent.Parent:FindFirstChild("Humanoid") or hit.Parent:FindFirstChild("Humanoid")
        if humanoid~=nil then
            local damage=math.random(BaseDamage-(BaseDamage*.1),BaseDamage+(BaseDamage*.1))
            if humanoid.Health>0 then
                local eplr=game.Players:playerFromCharacter(humanoid.Parent)
                local plr=game.Players:playerFromCharacter(Tool.Parent)
                if eplr~=nil and plr~=nil then
                if eplr.TeamColor~=plr.TeamColor or eplr.Neutral or plr.Neutral then
                        tagHumanoid(humanoid)
                        humanoid:TakeDamage(damage)
                end
                else
                    tagHumanoid(humanoid)
                    humanoid:TakeDamage(damage)
                end
            end
        end
        local distance=(startpoint-pos).magnitude
        bullet.CFrame=cfrm*CFrame.new(0,0,-distance/2)
        bullet.Mesh.Scale=Vector3.new(.15,.15,distance)
    else
        bullet.CFrame=cfrm*CFrame.new(0,0,-RayLength/2) 
        bullet.Mesh.Scale=Vector3.new(.15,.15,RayLength)
    end
    if pos~=nil then

    end

    bullet.Parent = workspace
    wait(0.1)
    bullet:remove()

end)

Closed as Not Constructive by User#19524

This question has been closed because it is not constructive to others or the asker. Most commonly, questions that are requests with no attempt from the asker to solve their problem will fall into this category.

Why was this question closed?