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

--Fixed, look at second answer--I'm trying to get a GUI script to work with two scripts....?

Asked by 4 years ago
Edited 4 years ago

I'm trying to change a GUI when a player dies, so I have a server script detect that. Then it send the event to a local script that's supposed to then change the GUI (I want the "lives" on the GUI to decrease when the player dies). But I'm getting errors, and only the server script prints anything. I have the remote event in repstorage, and my local script is in the GUI. My server script is in serverscriptstorage or whatever.

here the code from server script

01local player = game:GetService('Players').PlayerAdded:Connect(function(player)
02local char = player.CharacterAdded:Connect(function(character)
03local hum = character:WaitForChild("Humanoid")
04local repstorage = game:GetService("ReplicatedStorage")
05local DeathEvent = repstorage:WaitForChild("DeathEvent")
06 
07 
08 
09        hum.Died:connect(function()
10            print("player has died! detected from server")
11DeathEvent:FireClient(player)
12 
13        end)
14    end)
15 
16end)

this is the code from my LOCAL script

01local player = game:GetService('Players').PlayerAdded:Connect(function(player)
02local char = player.CharacterAdded:Connect(function(character)
03local hum = character:WaitForChild("Humanoid")
04 
05local DeathEvent = game.ReplicatedStorage.DeathEvent   
06local livestext = "lives: "
07local numberoflives = 3
08local lives = livestext .. numberoflives
09local text = script.Parent.Text
10 
11 
12local function deathfunction()
13            print("player has died! remote event worked")
14 
15            text = "is it working"
View all 21 lines...

Please explain your answers, I don't just want to get this to work, I want to learn and understand everything.

2 answers

Log in to vote
0
Answered by
DemGame 271 Moderation Voter
4 years ago
Edited 4 years ago

On your first script, everything is fine except for line 11. Instead of ":FireClient()", you are supposed to use ":FireAllClients()". Though this fires all clients instead of just one, you can always check whether you are the player who died using the local script since you are sending back the "player" variable through the ":FireAllCients()".

Here is your edited script:

01local player = game:GetService('Players').PlayerAdded:Connect(function(player)
02    local char = player.CharacterAdded:Connect(function(character)
03        local hum = character:WaitForChild("Humanoid")
04        local repstorage = game:GetService("ReplicatedStorage")
05        local DeathEvent = repstorage:WaitForChild("DeathEvent")
06 
07        hum.Died:connect(function()
08            print("player has died! detected from server")
09            DeathEvent:FireAllClients(player)
10        end)
11    end)
12end)

On your local script, you can just use

1local player = game.Players.LocalPlayer

instead, as it is in a local script. Otherwise, it should work.

Here is your edited local script:

01local localplayer = game:GetService('Players').LocalPlayer
02local char = localplayer.Character
03local hum = char:WaitForChild("Humanoid")
04local DeathEvent = game.ReplicatedStorage.DeathEvent   
05local livestext = "lives: "
06local numberoflives = 3
07local lives = livestext .. numberoflives
08local text = script.Parent.Text
09 
10DeathEvent.OnClientEvent:Connect(function(player) --condenses the creation of the function and connects it to the event in the same line
11    if player == localplayer then --Check if the player mentioned in the event is the same as the gui owner...
12        print("player has died! remote event worked")
13        text = "is it working"
14    end
15end)

Tell me if anything breaks.

Ad
Log in to vote
0
Answered by 4 years ago

@demcode, yes, your version partially worked. to get the text to change upon death I made it like this (local script, the server script was fine)

01local localplayer = game:GetService('Players').LocalPlayer
02local char = localplayer.Character
03local hum = char:WaitForChild("Humanoid")
04local DeathEvent = game.ReplicatedStorage.DeathEvent   
05local livestext = "lives: "
06local NumberOfLives = 3
07local livesGui = script.Parent
08 
09DeathEvent.OnClientEvent:Connect(function(player) --condenses the creation of the function and connects it to the event in the same line
10    if player == localplayer and NumberOfLives > 0 then --Check if the player mentioned in the event is the same as the gui owner...
11 
12        livesGui.Text = livestext .. tostring(NumberOfLives - 1)
13        NumberOfLives = NumberOfLives - 1
14 
15    elseif player == localplayer and NumberOfLives <= 0 then
16        livesGui.Text = "you have no more lives"
17    end
18end)

in my game once the player has no more lives I will teleport them.

Answer this question