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

Works, but how can I find the character that has shot the weapon from a server script?

Asked by 4 years ago
Edited 4 years ago
local Tool = script.Parent.Parent
debounce = false
local Muzzle = Tool.Muzzle.PointLight
local FireSound = script:WaitForChild("FireSound")
local HitSound = script:WaitForChild("HitSound")
local CS = game:GetService("CollectionService")
local player = game.Players.LocalPlayer



game.ReplicatedStorage.RayCastEvent.OnServerEvent:Connect(function(Player,FromP,ToP)
    local RayCast = Ray.new(FromP,(ToP-FromP).unit*100)
    local ignore = workspace:GetChildren();
    table.insert(ignore,player)
    local Part = game.Workspace:FindPartOnRayWithIgnoreList(RayCast,workspace.Camera:GetDescendants())
    FireSound:Play()
    if Part and debounce == false then
        local Hum = Part.Parent:FindFirstChild("Humanoid")
        Hum:TakeDamage("20")
        HitSound:Play()
        Muzzle.Enabled = true
        wait(0.1)
        debounce = true
        wait(0.1)
        Muzzle.Enabled = false
        debounce = false
    end
end)

Edit 2 : I have fixed my old issue, now though I need to find a way to make the script ignore local player (This is problematic because you cannot find character from a server script, so, is there anyway I can make this server script make sure that the ray ignores the player who shot the gun?

This is the local part where the event is fired

local UnequipEvent = game.ReplicatedStorage.Folder.Unequipped
local EquippedEvent = game.ReplicatedStorage.Folder.Equipped
local char = script.Parent
local player = game.Players.LocalPlayer
local Character = workspace.Camera:WaitForChild("Viewmodel")
local cam = workspace.Camera
local Mouse = game.Players.LocalPlayer:GetMouse()
local humanoid = Character:WaitForChild("Humanoid")
local Run = script:WaitForChild('Run')
local Fire = script:WaitForChild('Fire')
local FireAnimTPS = script:WaitForChild('FireAnimTPS')
local Muzzle = Character.Gun.FirePart.Muzzle
local MuzzleLight = Character.Gun.FirePart.PointLight
local FireSound = script.Fire:WaitForChild("FireSound")
local Reload = script:WaitForChild('Reload')
local ReloadTPS = script:WaitForChild("ReloadAnimTPS")
local ReloadSound = script.Reload:WaitForChild("ReloadSound")
local dance = humanoid:LoadAnimation(Run)
local FireAnim = humanoid:LoadAnimation(Fire)
local FireAnimTP = player.Character.Humanoid:LoadAnimation(FireAnimTPS)
local ReloadAnim = humanoid:LoadAnimation(Reload)
local ReloadAnimTPS = player.Character.Humanoid:LoadAnimation(ReloadTPS)
local Running = false
local Shooting = false
local Ammo = game.StarterPack.Gun.Ammo.Value
local Tool = game.StarterPack.Gun

debounce = false
Fired = false

EquippedEvent.OnClientEvent:Connect(function()
    Fired = true
end)
UnequipEvent.OnClientEvent:Connect(function()
    Fired = false
end)

player.Character.Humanoid.Running:connect(function(speed) --I hope player is a variable of an actual player.
        if speed <= 16 and humanoid.Jump == false then --If they are not walking,
                dance:Stop()
                Running = false
        elseif speed > 18 and humanoid.Jump == false then --If they started walking or is walking.
                dance:Play()
                FireAnim:Stop()
                MuzzleLight.Enabled = false
                 Running = true
        end
    end)

Mouse.Button1Down:Connect(function()
    if Running == false and Ammo > 0 and Fired == true then
        Shooting = true
        print("Shooting")
        FireAnimTP:Play()
        FireAnim:Play()
        repeat FireSound:Play() Ammo = Ammo - 1 game.Players.LocalPlayer:GetMouse().TargetFilter = game.Players.LocalPlayer.Character print(Ammo)game.ReplicatedStorage.RayCastEvent:FireServer(Tool.FirePart.Position, Mouse.Hit.Position)  Muzzle.Enabled = true MuzzleLight.Enabled = true wait(0.1) Muzzle.Enabled = false  until Ammo == 0 or Shooting == false or Running == true or Fired == false
        if Ammo <= 0 and Fired == true then
            Muzzle.Enabled = false
            FireAnim:Stop() 
            FireAnimTP:Stop()
            MuzzleLight.Enabled = false
            print("All out!")
            ReloadAnim:Play()
            ReloadAnimTPS:Play()
            ReloadSound:Play()
            Character.Gun.Magazine.Transparency = 1
            Character.Magazine1.Transparency = 0
            wait(1.3)
            Character.Magazine1.Transparency = 1
            Character.Gun.Magazine.Transparency = 0
            wait(1)
            ReloadSound:Stop()

            Ammo = 30
                    print("Loaded!")
        end
    end
end)

Mouse.Button1Up:Connect(function()
    Shooting = false
    print("Not Shooting")
    FireAnim:Stop()
    FireAnimTP:Stop()
    MuzzleLight.Enabled = false
end)


player.Character.Humanoid.Died:Connect(function()
    while player.Character.Humanoid.Health == 0 do
        Ammo = 30
        wait()
    end
end)

Mouse.KeyUp:connect(function (key) 
    key = string.lower(key)
if string.byte(key) == 48 then
        Running = false
        dance:Stop()
end
end)
    -- Run function

Mouse.Button2Down:Connect(function()
    Character.PrimaryPart = Character.AimHumanoid
    Aiming = true   
end)

Mouse.Button2Up:Connect(function()
    Character.PrimaryPart = Character.PartHumanoid
        Aiming = false
end)

1 answer

Log in to vote
0
Answered by
tomekcz 174
4 years ago
Edited 4 years ago

Before firing event in local script you need a line like that:

game.Players.LocalPlayer:GetMouse().TargetFilter = game.Players.LocalPlayer.Character
local UnequipEvent = game.ReplicatedStorage.Folder.Unequipped
local EquippedEvent = game.ReplicatedStorage.Folder.Equipped
local char = script.Parent
local player = game.Players.LocalPlayer
local Character = workspace.Camera:WaitForChild("Viewmodel")
local cam = workspace.Camera
local Mouse = game.Players.LocalPlayer:GetMouse()
local humanoid = Character:WaitForChild("Humanoid")
local Run = script:WaitForChild('Run')
010 local Fire = script:WaitForChild('Fire')
011 local FireAnimTPS = script:WaitForChild('FireAnimTPS')
012 local Muzzle = Character.Gun.FirePart.Muzzle
013 local MuzzleLight = Character.Gun.FirePart.PointLight
014 local FireSound = script.Fire:WaitForChild("FireSound")
015 local Reload = script:WaitForChild('Reload')
016 local ReloadTPS = script:WaitForChild("ReloadAnimTPS")
017 local ReloadSound = script.Reload:WaitForChild("ReloadSound")
018 local dance = humanoid:LoadAnimation(Run)
019 local FireAnim = humanoid:LoadAnimation(Fire)
020 local FireAnimTP = player.Character.Humanoid:LoadAnimation(FireAnimTPS)
021 local ReloadAnim = humanoid:LoadAnimation(Reload)
022 local ReloadAnimTPS = player.Character.Humanoid:LoadAnimation(ReloadTPS)
023 local Running = false
024 local Shooting = false
025 local Ammo = game.StarterPack.Gun.Ammo.Value
026 local Tool = game.StarterPack.Gun
027 Mouse.TargetFilter = {Character, game.Players.LocalPlayer.Character}

028 debounce = false
029 Fired = false
030  
031 EquippedEvent.OnClientEvent:Connect(function()
032     Fired = true
033 end)
034 UnequipEvent.OnClientEvent:Connect(function()
035     Fired = false
036 end)
037  
038 player.Character.Humanoid.Running:connect(function(speed) --I hope player is a variable of an actual player.
039         if speed <= 16 and humanoid.Jump == false then --If they are not walking,
040                 dance:Stop()
041                 Running = false
042         elseif speed > 18 and humanoid.Jump == false then --If they started walking or is walking.
043                 dance:Play()
044                 FireAnim:Stop()
045                 MuzzleLight.Enabled = false
046                  Running = true
047         end
048     end)
049  
050 Mouse.Button1Down:Connect(function()
051     if Running == false and Ammo > 0 and Fired == true then
052         Shooting = true
053         print("Shooting")
054         FireAnimTP:Play()
055         FireAnim:Play()
056         repeat FireSound:Play() Ammo = Ammo - 1 print(Ammo)game.ReplicatedStorage.RayCastEvent:FireServer(Tool.FirePart.Position, Mouse.Hit.Position)  Muzzle.Enabled = true MuzzleLight.Enabled = true wait(0.1) Muzzle.Enabled = false  until Ammo == 0 or Shooting == false or Running == true or Fired == false
057         if Ammo <= 0 and Fired == true then
058             Muzzle.Enabled = false
059             FireAnim:Stop()
060             FireAnimTP:Stop()
061             MuzzleLight.Enabled = false
062             print("All out!")
063             ReloadAnim:Play()
064             ReloadAnimTPS:Play()
065             ReloadSound:Play()
066             Character.Gun.Magazine.Transparency = 1
067             Character.Magazine1.Transparency = 0
068             wait(1.3)
069             Character.Magazine1.Transparency = 1
070             Character.Gun.Magazine.Transparency = 0
071             wait(1)
072             ReloadSound:Stop()
073  
074             Ammo = 30
075                     print("Loaded!")
076         end
077     end
078 end)
079  
080 Mouse.Button1Up:Connect(function()
081     Shooting = false
082     print("Not Shooting")
083     FireAnim:Stop()
084     FireAnimTP:Stop()
085     MuzzleLight.Enabled = false
086 end)
087  
088  
089 player.Character.Humanoid.Died:Connect(function()
090     while player.Character.Humanoid.Health == 0 do
091         Ammo = 30
092         wait()
093     end
094 end)
095  
096 Mouse.KeyUp:connect(function (key)
097     key = string.lower(key)
098 if string.byte(key) == 48 then
099         Running = false
100         dance:Stop()
101 end
102 end)
103     -- Run function
104  
105 Mouse.Button2Down:Connect(function()
106     Character.PrimaryPart = Character.AimHumanoid
107     Aiming = true  
108 end)
109  
110 Mouse.Button2Up:Connect(function()
111     Character.PrimaryPart = Character.PartHumanoid
112         Aiming = false
113 end)
 MuzzleLight.Enabled = true wait(0.1) Muzzle.Enabled = false  until Ammo == 0 or Shooting == false or Running == true or Fired == false
Ad

Answer this question