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

RemoteEvent LocalScript only works once?

Hello.

This localscript won't run again after the player resets. Therefore, the serverscript won't run because the event isn't fired.

How can you keep the localscript running, or at least, restarting, after resetting/dying?

UIS = game:GetService("UserInputService")
player = game.Players.LocalPlayer
inrange = false
dead = false

player.CharacterAdded:Connect(function()
	repeat wait() until player.Character
	dead = false
	local bloodstain = game.Workspace:FindFirstChild(player.Name.."_Bloodstain")
	if bloodstain ~= nil then
		while wait() do
			local magnitude = (player.Character.UpperTorso.Position - bloodstain.Position).Magnitude
			if magnitude <= 8 and not dead then
				inrange = true
				script.Parent.Visible = true
			else
				inrange = false
				script.Parent.Visible = false
			end
		end
	end
end)

player.CharacterAdded:Connect(function()
	repeat wait() until player.Character
	player.Character.Humanoid.Died:Connect(function()
		print(player.Name.." died")
		dead = true
		game.ReplicatedStorage.Events.died_event:FireServer(player)	
	end)	
end)

UIS.InputBegan:Connect(function(input,processed)
	if not processed then
		if input.UserInputType == Enum.UserInputType.Keyboard and inrange then
			local key = input.KeyCode
			if key == Enum.KeyCode.E then
				script.Parent.Visible = false
				game.ReplicatedStorage.Events.retreive_souls:FireServer(player)
			end
		end
	end
end)

If you need to see what is happening, you can search my name on Roblox and find my game called "Dead Souls". If you're trying it, please test out this script by resetting your character. Thank you for reading.

2 comments

0

Place the local script in the 'StarterPlayerScripts` folder (under StarterPlayer folder)

0

Why is this all happening in a local script in the first place? This is a lot of power given to the client   https://www.robloxdev.com/articles/Game-Security#server-side-validation

2 answers

UIS = game:GetService("UserInputService")
player = game.Players.LocalPlayer
inrange = false
dead = false

player.CharacterAdded:Connect(function()
    repeat wait() until player.Character
    dead = false
    local bloodstain = game.Workspace:FindFirstChild(player.Name.."_Bloodstain")
    if bloodstain ~= nil then
        while wait() do
            local magnitude = (player.Character.UpperTorso.Position - bloodstain.Position).Magnitude
            if magnitude <= 8 and not dead then
                inrange = true
                script.Parent.Visible = true
            else
                inrange = false
                script.Parent.Visible = false
            end
        end
    end


    repeat wait() until player.Character
    player.Character.Humanoid.Died:Connect(function()
        print(player.Name.." died")
        dead = true
        game.ReplicatedStorage.Events.died_event:FireServer(player) 
    end)   
end)



UIS.InputBegan:Connect(function(input,processed)
    if not processed then
        if input.UserInputType == Enum.UserInputType.Keyboard and inrange then
            local key = input.KeyCode
            if key == Enum.KeyCode.E then
                script.Parent.Visible = false
                game.ReplicatedStorage.Events.retreive_souls:FireServer(player)
            end
        end
    end
end)

Quick Hack, Untested... let me know if there any problems.

If it helped, please accept the answer.

5 comments

0

The script did not work.

0

The script runs clean on studio, but doesn't work in the actual game itself.

0

Hmm, a problem with FE then. Let me look.

0

Updated, use the new script and see if it works.

0

At least explain how it worked?

For future searchers, this was the solution.

LocalScript

UIS = game:GetService("UserInputService")
player = game.Players.LocalPlayer

player.CharacterAdded:Connect(function()
	player.Character:WaitForChild("Humanoid").Died:Connect(function()
		print(player.Name.." died")
		game.ReplicatedStorage.Events.died_event:FireServer(player)
	end)

	game.ReplicatedStorage.Events.died_event.OnClientEvent:Connect(function(inrange)
		if inrange == true then
			player.PlayerGui.ScreenGui.Bloodstain.Visible = true
		else
			player.PlayerGui.ScreenGui.Bloodstain.Visible = false
		end
	end)
	
	UIS.InputBegan:Connect(function(input,processed)
		local collectible = false
		if not processed then
			if input.UserInputType == Enum.UserInputType.Keyboard then
				local key = input.KeyCode
				if key == Enum.KeyCode.E and player.Character.Humanoid.Health > 0 and player.PlayerGui.ScreenGui.Bloodstain.Visible == true then
					player.PlayerGui.ScreenGui.Bloodstain.Visible = false
					game.ReplicatedStorage.Events.retreive_souls:FireServer(player)
				end
			end
		end
	end)
end)

ServerScript

collected = false

game.ReplicatedStorage.Events.died_event.OnServerEvent:Connect(function(player)
	if game.Workspace:FindFirstChild(player.Name.."_Bloodstain") then
		local existingbloodstain = game.Workspace:FindFirstChild(player.Name.."_Bloodstain")
		existingbloodstain.Position = player.Character.LeftFoot.Position
		existingbloodstain.Souls.Value = player.Souls.Value
		player.Souls.Value = 0
		game.ReplicatedStorage.Events.died_event:FireClient(player,existingbloodstain)
	else
		local bloodstain = game.ReplicatedStorage.Bloodstain:Clone()
		bloodstain.Parent = game.Workspace
		bloodstain.Name = (player.Name.."_Bloodstain")
		bloodstain.Position = player.Character.LeftFoot.Position
		bloodstain.Souls.Value = player.Souls.Value
		player.Souls.Value = 0
	end
	
	collected = false
	local bloodstain = game.Workspace:FindFirstChild(player.Name.."_Bloodstain")
	
	while wait() do
		local magnitude = (player.Character.UpperTorso.Position - bloodstain.Position).Magnitude
		local inrange = false
		
		if magnitude <= 8 and player.Character.Humanoid.Health > 0 and not collected then
			inrange = true
			game.ReplicatedStorage.Events.died_event:FireClient(player,inrange)
		else
			inrange = false
			game.ReplicatedStorage.Events.died_event:FireClient(player,inrange)
		end
	end
end)

game.ReplicatedStorage.Events.retreive_souls.OnServerEvent:Connect(function(player)
	local bloodstain = game.Workspace:FindFirstChild(player.Name.."_Bloodstain")
	if bloodstain ~= nil then
		collected = true
		player.Souls.Value = player.Souls.Value + bloodstain.Souls.Value
		bloodstain.Souls.Value = 0
		bloodstain.retreive:Play()
		for i=0,1,0.05 do
			wait()
			bloodstain.Transparency = i
		end
		bloodstain.retreive.Ended:Connect(function()
			bloodstain.Position = game.Workspace.Bloodstain_Position.Position
			bloodstain.Transparency = 0
		end)
	end
end)


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