Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

So does anyone know how to "Reload" a character using a script?

So I have a script called "GravityController" that is being destroyed in the script when a player touches the part "Sand". Once the "GravityController" script is destroyed then it will reload the character essentially. So does anyone know how to "Reload" a character using a script?

local Sand = game.Workspace:WaitForChild("Sand")
local Debounce = false
 Sand.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
    if Debounce == false then Debounce = true
         game.ReplicatedStorage.GravityController:Destroy()
         print("Module successfully destroyed")
    	end
    end
end)

local player = workspace:FindFirstChild("Player")
ResetPoint = CFrame.Position
player.Position = Vector3.new(ResetPoint)

if game.ReplicatedStorage.GravityController:Destroy() then
	local part = workspace.FindFirstChild("Sand")
	part.Anchored = true
	part.Parent = workspace
	part.Touched:connect(function(hit)
	local hum = hit.Parent:FindFirstChild("Humanoid")
	if hum then
		hum.Health = 0 
	end
end)
end

local part = workspace.FindFirstChild("Sand")
part.Touched:connect(function(hit)
local hum = hit.Parent:FindFirstChild("Humanoid")

if hum.Health = 0 and game.ReplicatedStorage.GravityController:Destroy() then
	player.Position = Vector3.new(ResetPoint)
	end
end)

2 comments

0

Instance:Destroy() returns nothing so the if statement never runs.

1 answer

If what you want is that when you touch Sand, GravityController is destroyed and reloads the character you can do this. This assumes you use a Script.

local PlayerService = game:GetService("Players")
local Sand = workspace:WaitForChild("Sand") --no need to use game.Workspace, workspace is fine.


local Connection
Connection = Sand.Touched:Connect(function(Part)
	--Get player that touches the part.
	local Player = PlayerService:GetPlayerFromCharacter(Part.Parent)
	if Player then
		Connection:Disconnect() --disconnect connection so that Sand does not check for touches again. You can use debounce, but in this case it's probably not needed.
		local ResetPoint = Part.Parent:GetPrimaryPartCFrame()
		game:GetService("ReplicatedStorage").GravityController:Destroy()
		Player:LoadCharacter()		
	
		local Character = Player.Character or Player.CharacterAdded:Wait()
		Character:SetPrimaryPartCFrame(ResetPoint)
	end
end)

If you are using a LocalScript, you need to use remote events to reload the character or just set the character position directly into the ResetPosition. If you use LocalScript, you cannot destroy GravityController because the server still sees it.

This discussion is preserved as it was. New answers, comments, and votes are closed.