Can't save and revert positions and health of player

The Goal

  • The player clicks a button from their GUI
  • It fires a RemoteEvent that the server receives
  • The server then saves the position and heath of the player
  • Once that’s done it kills the player
  • Then once the player has spawned, it puts the player back to its original position and saved health.

The Problem

Now, I don’t have a clue what the issue is other than the fact that it might be ROBLOX’s fault because I tried so many different things like a module that gets the health of the player and even tried firing remotes to get it and the position too, it just won’t work.

Details

  • I did damage the player (using console) before pressing the button, but the printed output was still 100 (on the server)
  • No errors or warnings at all
  • Script below is in a script not a localscript
  • All the “checks” with ‘ran’ and ‘ok’ prints print out perfectly fine
--[ Variables ]--
local FixSaber = game.ReplicatedStorage.Server.FixSaber
--[ Core ]--
FixSaber.OnServerEvent:Connect(function(plr)
	
	local Humanoid = plr.Character.Humanoid
	local Torso = plr.Character.Torso
	local LastHealth = Humanoid.Health
	local LastPosition = Torso.CFrame
	
	Humanoid.Health = 0
	
	plr.CharacterAdded:Connect(function(char)
		
		print('ran')
		char:WaitForChild('Torso').CFrame = LastPosition
		print('ok')
		char:WaitForChild('ForceField'):Destroy()
		print('oK')
		char:WaitForChild('Humanoid').Health = LastHealth
		print('OK')
		return
		
	end)
	
end)
2 Likes

Not sure as to why it’s not working, theoretically it should, but there’s a potential issue, everytime you fire the event, you’re connecting a new function to CharacterAdded, what you should instead do is wait for the event to fire:

--[ Core ]--
FixSaber.OnServerEvent:Connect(function(client)
	local character = client.Character
	local humanoid = character.Humanoid
	local torso = character.Torso
	local prevHealth = humanoid.Health
	local prevPos = torso.CFrame
	humanoid.Health = 0

	client.CharacterAdded:Wait()
       wait(0.1) 
	print("character reloaded, now putting them back where they were...")
	torso.CFrame = prevPos
	print("giving them their previous health")
	humanoid.Health = prevHealth
	print("Done")
end)
1 Like

I believe the only thing that script you provided will only prevent the amount of functions being rapidly created which shouldn’t be an issue even because it has the return function that stops the function entirely. I still don’t know why my script doesn’t work.

1 Like

All functions have an either implicit or explicit return statement at the end of the body. I don’t know what you’re talking about?

All I said was a potential issue being you’re creating new connections each time the remote event is fired, so it’s fired twice for example, you get two connections since you are connecting 2 different functions. (Lua passes objects by reference,that is, tables, userdatas, threads and functions by reference, function() end == function() end evaluates to false for example.)

Could you maybe try calling wait before changing the properties? I’ve updated my post to show where to wait. Maybe it’s not working since it was still in the process of respawning, but not too sure

1 Like

But then I also used waitforChild() anyways so I don’t think it makes a difference on reliability?

by the way

Was able to make it work, WaitForChild(‘Torso’) didn’t work out because when the original Torso spawns it can’t be moved instantly because it still has to load in and then I knew there was something up with my Health script and found out it wasn’t the script it was because the command console was client-based because I had my side to client instead of server

2 Likes