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

2 different scripts read the same value as different?

Asked by 1 year ago

The Enemy is supposed to have their CurrentAction change from "AttackLeft" to "Flinch" when hit, but it only seems to read as "Flinch" on one script while the other script still reads it as "AttackLeft".

Video: https://streamable.com/j4f2l2

Script 1 (server script):

local UserInputService = game:GetService("UserInputService")
local character = script.Parent.Parent
local debounce = script.Parent.Debounce.Value
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")
local swordDamage = script.Parent.WeaponDamageBindableEvent
local beingUsed = script.Parent.BeingUsedBindableEvent
local CurrentAction = script.Parent.Parent.CurrentAction.Value

--[[local AudioPlayerModule = require(ReplicatedStorage:WaitForChild("AudioPlayer")
AudioPlayerModule.preloadaudio({

})]]

--[[
11768323667 right
11768327293 up
11768331665 left
]]

repeat wait() until character.Humanoid
local humanoid = character.Humanoid
local anim = Instance.new("Animation")
anim.Parent = humanoid
anim.AnimationId = "https://www.roblox.com/asset/?id=11779584892"
local playAnim = humanoid:LoadAnimation(anim)

while true do
    wait()
    print(CurrentAction)
    if CurrentAction == "AttackLeft" and not playAnim.IsPlaying then
        CurrentAction = nil
    end
if debounce ~= true and CurrentAction ~= "Flinch" and not playAnim.IsPlaying then
        debounce = true
        playAnim:Play(0, 1, 1)
        CurrentAction = "AttackLeft"
        local swordDamageCoroutine = coroutine.create(function()
            swordDamage:Fire(3, true, 0.7, 0.2, "AttackLeft")
        end)
        coroutine.resume(swordDamageCoroutine)
        --sound goes here
        --AudioPlayerModule.playAudio("")
    local proceed = false
    flinchCalculator = RunService.Heartbeat:Connect(function(deltaTime)
        if playAnim.IsPlaying == false then proceed = true end
        if CurrentAction ~= "AttackLeft" then proceed = true end
        if proceed == true then
            if CurrentAction ~= "Flinch" then CurrentAction = nil end
            playAnim:Stop()
            swordDamage:Fire(0, false, false, false)
            beingUsed:Fire()
            debounce = false
            flinchCalculator:Disconnect()
            wait(0.5)
        end
    end)
end
end

Script 2 (server script):

local UserInputService = game:GetService("UserInputService")
local character = script.Parent.Parent
local debounce = false
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local swordDamage = ReplicatedStorage.SwordDamageRemoteFunction
local beingUsed = ReplicatedStorage.BeingUsedRemoteFunction
local tookDamage = script.Parent.TookDamageBindableEvent
local CurrentAction = script.Parent.Parent.CurrentAction.Value

repeat wait() until character.Humanoid
local humanoid = character.Humanoid
local anim = Instance.new("Animation")
anim.Parent = humanoid
anim.AnimationId = "https://www.roblox.com/asset/?id=11779381761"
local playAnim = humanoid:LoadAnimation(anim)

tookDamage.Event:Connect(function(flinch, flinchTime)
    if flinch then
        playAnim:Play(0, 1, 1)
        CurrentAction = "Flinch"
        print(CurrentAction)
        wait(flinchTime)
        CurrentAction = nil
    end
end)

1 answer

Log in to vote
2
Answered by 1 year ago
Edited 1 year ago

local CurrentAction = script.Parent.Parent.CurrentAction.Value

Something I noticed immediately, you are hard storing the value of CurrentAction to the variable. Because you are hard storing the value to the variable, doing CurrentAction = "Flinch" --or "AttackLeft" will only change the variable instead of the value of the instance.

If you want to update the value of the instance, you need to use .Value when assigning its new value.

local CurrentAction = script.Parent.Parent.CurrentAction
CurrentAction.Value = "Flinch" --or "AttackLeft"

It is best to use a function for these.

local CurrentAction = script.Parent.Parent.CurrentAction

local function UpdateAction(action)
    CurrentAction.Value = action
end
Ad

Answer this question