LocalScript not completing when fired from Server RemoteEvent upon character death/respawn

I have 3 scripts that (together) determine if a player kills another player. If it does then it announces the kill to the entire server and processes a bounty, etc. The entire script chain works perfectly, except for the player who was killed. Weirdly, the final script partially runs and then hangs on the last step.

LocalScript to Determine Character Death & Fire Server Event (in StarterCharacterScripts)

local Players = game:GetService("Players")
local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local hitbox = character:WaitForChild("playerHitbox")
local replicatedStorage = game:GetService("ReplicatedStorage")
local events = replicatedStorage:WaitForChild("Events")
local touchEvent = events:WaitForChild("touchEvent")

local db = true

local function touchPlayer(object)
	
	if db then
		db = false
		if object.Parent.Name == "target" then
			touchEvent:FireServer()
			task.wait(5)
		end
		task.wait(1)
		db = true		
	end
end

hitbox.Touched:Connect(touchPlayer)

Script to Kill Player, Award Points, and Fire RemoteEvent back to All Clients (in ServerScriptService)

local repStore = game:GetService("ReplicatedStorage")
local events = repStore:FindFirstChild("Events")
local touchEvent = events:WaitForChild("touchEvent")
local killedEvent = events:WaitForChild("killedEvent")
local dss = game:GetService("DataStoreService")
local gameVersion = "1"

local function killPlayer(player)
	
	local Players = game:GetService("Players")
	local targetCharacter = game.Workspace:WaitForChild("target")
	local targetPlayer = Players:GetPlayerFromCharacter(targetCharacter)
	local targetPlayerUserID = targetPlayer.UserId

	local killerBounty = player:WaitForChild("PlayerStats"):WaitForChild("Bounty")
	local killerCash = player:WaitForChild("leaderstats"):WaitForChild("Cash")

	local targetPlayerStats = targetPlayer:WaitForChild("PlayerStats")
	local targetPlayerLeaderstats = targetPlayer:WaitForChild("leaderstats")
	local targetPlayerCash = targetPlayerLeaderstats:WaitForChild("Cash")
	local targetPlayerBounty = targetPlayerStats:WaitForChild("Bounty")
	local targetHumanoid = targetCharacter:FindFirstChild("Humanoid")
	local cashStolen = math.floor(targetPlayerCash.Value / 2)
	local totalBounty = targetPlayerBounty.Value + cashStolen

	targetPlayerCash.Value = targetPlayerCash.Value - cashStolen
	killerBounty.Value = killerBounty.Value + math.floor(targetPlayerBounty.Value / 4)
	killerCcash.Value = killerCash.Value + totalBounty
	
	local playerData = dss:GetDataStore(gameVersion)
	local data = {
		["Cash"] = targetPlayerCash.Value
	}
	
	playerData:SetAsync(targetPlayerUserID,data)
	
	targetHumanoid.Health = 0
		
	killedEvent:FireAllClients(player,targetPlayer,targetPlayerBounty.Value,cashStolen)
		
end

touchEvent.OnServerEvent:Connect(killPlayer)

LocalScript to Broadcast Kill to All Players (in StarterGui)

local replicatedStorage = game:GetService("ReplicatedStorage")
local events = replicatedStorage:WaitForChild("Events")
local killedEvent = events:WaitForChild("killedEvent")
local gameVersion = "1"

local player = game.Players.LocalPlayer
local playerGui = player:WaitForChild("PlayerGui")
local playerSystemMsg = playerGui:WaitForChild("systemMessage"):WaitForChild("Frame"):WaitForChild("systemMsgLabel")
local playerBanner = playerGui:WaitForChild("Banner"):WaitForChild("Frame"):WaitForChild("bannerLabel")

local function playerKilled(playerKiller,targetKilled,targetPlayerBounty,cashStolen)
	
	playerSystemMsg.Text = tostring(playerKiller).." killed "..tostring(targetKilled)..", collecting a Bounty of "..tostring(targetPlayerBounty).." and stealing "..tostring(cashStolen).." Cash!"
	playerSystemMsg.Visible = true
	task.wait(5)
	playerSystemMsg.Visible = false
		
end

killedEvent.OnClientEvent:Connect(playerKilled)

This single line from the last script runs for all Clients except the player that was killed.

	playerSystemMsg.Visible = false

Essentially, the broadcast message appears, and then disappears for all players. On the player that died, it appears, but never goes away.

Check the ScreenGui in which the playerSystemMsg is located and make sure ResetOnSpawn is turned to false.

1 Like

I double-checked and it is set to false.

1 Like

Could you show me your explorer for where the script and gui are located?

image

Change your StarterGui LocalScript to this:

--//Services
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

--//Variables
local events = ReplicatedStorage:WaitForChild("Events")
local killedEvent = events:WaitForChild("killedEvent")
local playerGui = script.Parent
local playerSystemMsg = playerGui:WaitForChild("systemMessage"):WaitForChild("Frame"):WaitForChild("systemMsgLabel")

--//Functions
killedEvent.OnClientEvent:Connect(function(playerKiller, targetKilled, targetPlayerBounty, cashStolen)
	print("Displayed message")
	
	playerSystemMsg.Text = playerKiller.Name .." killed "
		.. targetKilled.Name ..", collecting a Bounty of "
		.. targetPlayerBounty .." and stealing "
		.. cashStolen .." Cash!"
	
	playerSystemMsg.Visible = true
	
	task.wait(5)
	playerSystemMsg.Visible = false
end)

And your server script to this:

--//Services
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

--//Variables
local playerData = DataStoreService:GetDataStore("1")
local events = ReplicatedStorage:WaitForChild("Events")
local touchEvent = events:WaitForChild("touchEvent")
local killedEvent = events:WaitForChild("killedEvent")

--//Functions
touchEvent.OnServerEvent:Connect(function(player)
	local targetCharacter = workspace:WaitForChild("target")
	local targetPlayer = Players:GetPlayerFromCharacter(targetCharacter)
	local targetPlayerUserID = targetPlayer.UserId

	local killerBounty = player:WaitForChild("PlayerStats"):WaitForChild("Bounty")
	local killerCash = player:WaitForChild("leaderstats"):WaitForChild("Cash")

	local targetPlayerStats = targetPlayer:WaitForChild("PlayerStats")
	local targetPlayerLeaderstats = targetPlayer:WaitForChild("leaderstats")
	local targetPlayerCash = targetPlayerLeaderstats:WaitForChild("Cash")
	local targetPlayerBounty = targetPlayerStats:WaitForChild("Bounty")
	local cashStolen = math.floor(targetPlayerCash.Value / 2)
	local totalBounty = targetPlayerBounty.Value + cashStolen

	targetPlayerCash.Value -= cashStolen
	killerBounty.Value += math.floor(targetPlayerBounty.Value / 4)
	killerCash.Value += totalBounty
	
	killedEvent:FireAllClients(player, targetPlayer, targetPlayerBounty.Value, cashStolen)

	playerData:SetAsync(targetPlayerUserID, {
		Cash = targetPlayerCash.Value
	})
	
	local targetHumanoid = targetCharacter:FindFirstChild("Humanoid")
	
	if targetHumanoid then
		targetHumanoid.Health = 0
	end
end)

Tell me what it prints when you from that touched event.

It printed “Displayed Message”.

Did it work then? Also, were there any errors?

No errors but it didnt work. Same exact result. The systemMessage text was enabled but then never disabled afterward.

Could be a problem with it being fired twice if someone dies multiple times, which might make the message appear again.

Change your LocalScript to this and test it again:

--//Services
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

--//Variables
local events = ReplicatedStorage:WaitForChild("Events")
local killedEvent = events:WaitForChild("killedEvent")
local playerGui = script.Parent
local playerSystemMsg = playerGui:WaitForChild("systemMessage"):WaitForChild("Frame"):WaitForChild("systemMsgLabel")

--//Controls
local messageDebounce = false

--//Functions
killedEvent.OnClientEvent:Connect(function(playerKiller, targetKilled, targetPlayerBounty, cashStolen)
	if messageDebounce then
		return
	end
	
	messageDebounce = true

	playerSystemMsg.Text = playerKiller.Name .." killed "
		.. targetKilled.Name ..", collecting a Bounty of "
		.. targetPlayerBounty .." and stealing "
		.. cashStolen .." Cash!"

	playerSystemMsg.Visible = true

	task.wait(5)
	print("Set to invisible")
	playerSystemMsg.Visible = false
	
	messageDebounce = false
end)

Set to Invisible triggers on the killer player but not the killed player. So weird though, playerSystemMsg.Visible = true, IS working on both killer and target guis.

You are right if multiple kills are firing the event, and I’ll need to code around that eventually. However, I’m only testing with 2-players at the moment and getting this.

Here’s something promising…
I changed the bottom of the LocalScript to this and both print statements triggered on both clients.
Commenting out the task.wait made the script run all the way through, just couldn’t see it happen on the screen.

	playerSystemMsg.Visible = true

	--task.wait(5)
	print("Set to invisible")
	playerSystemMsg.Visible = false
	print("worked")

	messageDebounce = false
end)

Yea that’s really weird then. No clue why that would happen. Maybe you can change the wait value to something else to test and see if it still does the same.

Yeah I’ll try that. Going to hit the bed for the night but will try it in the morn and let you know how it goes. Thanks for all your help on this man.

1 Like

Okay, good luck on fixing your problem.

So I decided to try it a different way this morning.

I rewrote the variables and Enable/Disabled the ScreenGui instead of setting the textLabel Visibility. I also changed the ResetOnSpawn to true for the screenframe since only the target would be dying/respawning. This worked perfectly, and even waited the full 5 seconds for some reason (I would have thought it would turn off immediately on respawn.

Weird that the ScreenGui would work but not the textLabel.
Here was the final code. And thank you again for all your help!!

--//Services
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

--//Variables
local events = ReplicatedStorage:WaitForChild("Events")
local killedEvent = events:WaitForChild("killedEvent")
local playerGui = script.Parent
local playerSystemMsg = playerGui:WaitForChild("systemMessage")
local playerSystemMsgText = playerSystemMsg:WaitForChild("Frame"):WaitForChild("systemMsgLabel")

--//Controls
local messageDebounce = false

--//Functions
killedEvent.OnClientEvent:Connect(function(playerKiller, targetKilled, targetPlayerBounty, cashStolen)
	if messageDebounce then
		return
	end

	messageDebounce = true

	playerSystemMsgText.Text = playerKiller.Name .." killed "
		.. targetKilled.Name ..", collecting a Bounty of "
		.. targetPlayerBounty .." and stealing "
		.. cashStolen .." Cash!"

	playerSystemMsg.Enabled = true

	task.wait(5)
	print("Set to invisible")
	playerSystemMsg.Enabled = false
	print("worked")

	messageDebounce = false
end)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.