-- Background, not really needed for the question but can make it easier to understand
I'm working on a shooter and beneath is the script that will run if a bullet hit is detected. The script works fine but I'm wondering about the remote event used in it. As this is written in a local script, I've created the remote event TakeDamage which will apply the damage taken to the target. However, this takes a few seconds. At first, I didn't want to stop the script to wait for the dmg to be applied is I worried this would slow down the guns, something I've now realized is fine as the spawn function will make sure that the rest of the script will run as normal. But as I didn't realize this until later, I've tried to simulate the dmg locally before running the rest to make sure that everything works.
-- Question
Is there any way to activate a function or to stop the script until a remote event has finished? I know that I could just use a normal wait statement but I would have to make it unnecessary long to make sure that lag can't ruin it.
-- Script
spawn(function()
events.TakeDamage:FireServer(hit.Parent, hit, stats)
--// Simulate hit for player
local dmg = stats.dmg
if stats.dmgmultipliers[hit.Name] then
dmg = dmg * stats.dmgmultipliers[hit.Name]
end
--// See if the damage is enough to kill the character
if hit.Parent.Humanoid.Health - dmg <= 0 then
hit.Parent.Humanoid.Health = 0
--// If so, then update cash and apply headshot multiplier
local cashonkill = stats.cashonkill
if hit.Name == "Head" then
cashonkill = cashonkill * stats.headshotbonus
end
cashFrame.Cash.WaitTime.Value = 1
plr.PlayerData.Cash.Value = plr.PlayerData.Cash.Value + cashonkill
ClientBasedFunctions:AnimateCash(plr, UDim2.new(0, plrMouse.X, 0, plrMouse.Y))
end
--// Display damage
ClientBasedFunctions:DisplayDamage(plr, hit.Parent, dmg)
end)
