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

My RemoteEvent is firing, but my script is not working properly?

Asked by
Phyb 28
5 years ago

I have a LocalScript inside the player(s), which records if a player presses, and releases a certain key, which in this case, is Z. Pressing the key fires the GomuPistolEvent and KeyHeld = true, releasing the key fires the GomuPistolEvent and KeyHeld = false. My problem is that the script I have in ServerScriptService that is supposed to check for the event runs the remote the first time, during the "if KeyHeld then", but not the second time, during the "elseif not KeyHeld then". Could anyone explain what am I doing wrong?

Thanks!

LocalScript:

--//Services
local UserInputService = game:GetService('UserInputService')
local ReplicatedStorage = game:GetService('ReplicatedStorage')
local ServerStorage = game:GetService('ServerStorage')
local ServerScriptService = game:GetService('ServerScriptService')
local Players = game:GetService('Players')
local GomuRemotes = ReplicatedStorage.Remotes.DevilFruits:WaitForChild('GomuGomu')

--//Variables
local GomuPistolEvent = GomuRemotes.GomuPistol:WaitForChild('GomuPistolEvent')
local Player = game.Players.LocalPlayer
local KeyHeld = true
local Debounce = false

--//Coding
function onKeyPress(input, gameEvent)
    if not Debounce and not gameEvent then
        Debounce = true
        if input.KeyCode == Enum.KeyCode.Z then
            KeyHeld = true
            GomuPistolEvent:FireServer(Player, KeyHeld)
            print('Z')
        end
        if input.KeyCode == Enum.KeyCode.X then
        end
        if input.KeyCode == Enum.KeyCode.C then
        end
        if input.KeyCode == Enum.KeyCode.V then
        end
        if input.KeyCode == Enum.KeyCode.B then
        end
        Debounce = false
    end
end
function onKeyRelease(input, gameEvent)
    if input.KeyCode == Enum.KeyCode.Z then
        KeyHeld = false
        GomuPistolEvent:FireServer(Player, KeyHeld)
        print('ZRelease')
    end
    if input.KeyCode == Enum.KeyCode.X then
    end
    if input.KeyCode == Enum.KeyCode.C then
    end
    if input.KeyCode == Enum.KeyCode.V then
    end
    if input.KeyCode == Enum.KeyCode.B then
    end
end

UserInputService.InputBegan:Connect(onKeyPress)
UserInputService.InputEnded:Connect(onKeyRelease)

ServerScript:

--//Services
local UserInputService = game:GetService('UserInputService')
local ReplicatedStorage = game:GetService('ReplicatedStorage')
local ServerStorage = game:GetService('ServerStorage')
local ServerScriptService = game:GetService('ServerScriptService')
local Players = game:GetService('Players')
local GomuRemotes = ReplicatedStorage.Remotes.DevilFruits:WaitForChild('GomuGomu')

--//Variables
local GomuPistolEvent = GomuRemotes.GomuPistol:WaitForChild('GomuPistolEvent')
local DamageModule = require(ServerStorage.Server.DamageService)

--//Animations
local GomuPistolAnim = Instance.new('Animation')
GomuPistolAnim.AnimationId = 'rbxassetid://2188557970'
GomuPistolAnimTrack = nil

--//Coding
GomuPistolEvent.OnServerEvent:Connect(function(Player, KeyHeld)
    local Character = game.Workspace:WaitForChild(Player.Name)
    local Humanoid = Character:FindFirstChildOfClass('Humanoid')
    local RightArm = Character:WaitForChild('RightLowerArm')
    local RightHand = Character:WaitForChild('RightHand')
    local LeftArm = Character:WaitForChild('LeftLowerArm')
    local LeftHand = Character:WaitForChild('LeftHand')
    local Mouse = Player:GetMouse()
    local MousePosition = Mouse.Hit.p
    local Damaging = false
    local Attacking = false
    local Damage = nil
    local Cooldown = nil
    local Range = nil
    local BodyGyro = nil
    local BodyPosition = nil
    local ArmStretch = nil
    local Weld = nil

    if KeyHeld then
        GomuPistolAnimTrack = Humanoid:LoadAnimation(GomuPistolAnim)
        GomuPistolAnimTrack:Play()
        GomuPistolAnimTrack.KeyframeReached:Connect(function(Keyframe)
            if KeyHeld and Keyframe == 'Charge' then
                GomuPistolAnimTrack:AdjustSpeed(0)
            end
        end)
        print('pistolcharge')
        Attacking = true
        BodyGyro = Instance.new('BodyGyro', Character.LowerTorso)
        BodyGyro.Name = 'SkillGyro'
        BodyGyro.D = 100
        BodyGyro.MaxTorque = Vector3.new(math.huge, math.huge, math.huge)
        BodyPosition = Instance.new('BodyPosition', Character.LowerTorso)
        BodyPosition.Name = 'SkillPosition'
        BodyPosition.Position = Character.HumanoidRootPart.Position
        BodyPosition.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
        while KeyHeld do
            MousePosition = Mouse.Hit.p
            BodyGyro.CFrame = CFrame.new(Character.LowerTorso.Position, MousePosition) * CFrame.Angles(0, math.rad(-90) ,0)
            wait()
        end
    elseif not KeyHeld then
        GomuPistolAnimTrack:AdjustSpeed(1)
        print('pistol')
        BodyGyro.CFrame = CFrame.new(Character.LowerTorso.Position, MousePosition)
        GomuPistolAnimTrack.KeyframeReached:Connect(function(Keyframe)
            if Keyframe == 'Pistol' then
                Damaging = true
                ArmStretch = Instance.new('Part', Character)
                ArmStretch.Name = 'ArmStretchRight'
                ArmStretch.Color = RightArm.Color
                ArmStretch.Size = RightArm.Size
                ArmStretch.CanCollide = false
                RightArm.Transparency = 1
                RightHand.Transparency = 1
                Weld = Instance.new('Weld', ArmStretch)
                Weld.Part0 = ArmStretch
                Weld.Part1 = RightArm
                Weld.C0 = CFrame.new(0, 0, 0) * CFrame.new(0, 0, 0)
            end
            if Keyframe == 'End' then
                Character.LowerTorso:WaitForChild('SkillGyro'):Destroy()
                Character.LowerTorso:WaitForChild('SkillPosition'):Destroy()
                Character:WaitForChild('ArmStretchRight'):Destroy()
                if RightArm.Transparency == 1 then RightArm.Transparency = 0 end
                if RightHand.Transparency == 1 then RightHand.Transparency = 0 end
                BodyGyro = nil
                BodyPosition = nil
                ArmStretch = nil
                Weld = nil
                Attacking = false
            end
        end)
    end

    RightArm.Touched:Connect(function(hitPart)
        if Attacking and hitPart.Parent:FindFirstChild('Humanoid') and Damaging then
            Damaging = false
            local HumanoidToDamage = hitPart.Parent:FindFirstChild('Humanoid')
            DamageModule.Combat(HumanoidToDamage, Damage)
        end
    end)
    LeftArm.Touched:Connect(function(hitPart)
        if Attacking and hitPart.Parent:FindFirstChild('Humanoid') and Damaging then
            Damaging = false
            local HumanoidToDamage = hitPart.Parent:FindFirstChild('Humanoid')
            DamageModule.Combat(HumanoidToDamage, Damage)
        end
    end)
end)

1 answer

Log in to vote
1
Answered by 5 years ago

i know exactly why, and, i'm not gonna lie, i've ran into this a few times myself;

it's line 21 and 38; this.

GomuPistolEvent:FireServer(Player, KeyHeld)

now, when the client fires an event to the server, it already knows which client sent it, therefore, you don't need to include Player in this FireServer(). Player automatically is assigned to the first argument in the server function, while everything detailed in FireServer() comes after, if that makes sense.

hope i helped!

0
Correct!!! User#19524 175 — 5y
0
This did help, thank you so much. I just had to add "PlayerWhoSent" in the function arguments on the serverscript since the variables call for the name of the player, and their mouse. I tested it, and sure enough it worked. Phyb 28 — 5y
Ad

Answer this question