Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

RemoteEvent LocalScript only works once?

Asked by
iTamago 18
5 years ago
Edited 5 years ago

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.

0
Place the local script in the 'StarterPlayerScripts` folder (under StarterPlayer folder) awesomeipod 607 — 5y
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 Vulkarin 581 — 5y

2 answers

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago
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.

0
The script did not work. iTamago 18 — 5y
0
The script runs clean on studio, but doesn't work in the actual game itself. iTamago 18 — 5y
0
Hmm, a problem with FE then. Let me look. AlphaGamer150 101 — 5y
0
Updated, use the new script and see if it works. AlphaGamer150 101 — 5y
0
At least explain how it worked? User#19524 175 — 5y
Ad
Log in to vote
0
Answered by
iTamago 18
5 years ago
Edited 5 years ago

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)


Answer this question