I want to get the recieved value from a Remote Event out of the function so I can use it everywhere in the script, but I don’t know how that would work.
This is the LocalScript:
local RE = game:GetService("ReplicatedStorage"):WaitForChild("4590863")
RE:FireServer(game.Players.LocalPlayer.Name)
And this is the Script:
RE1.Parent = game:GetService("ReplicatedStorage")
RE1.Name = "4590863"
local function REfired(plr)
local s = game:GetService("ServerStorage"):WaitForChild("Sound")
local xy = s:Clone()
xy.Parent = game:GetService("ServerStorage")
local xplr = tostring(plr)
s.Parent = workspace[xplr].Head
end
RE1.OnServerEvent:Connect(REfired)
I want the player name that was received, so I can play the created sound serversided when the player dies.
x = plr name from the remote event
workspace[x].Humanoid.Died:Connect(function()
Sound:Play()
end)
If you’re just looking to get the player name, you don’t need to put the game.Players.LocalPlayer.Name inside the FireServer(). Instead, do the FireServer() with nothing inside the parenthesis and on the server change tostring(plr) to plr.Name.
-- LocalScript
local RE = game:GetService("ReplicatedStorage"):WaitForChild("4590863")
RE:FireServer()
-- Script
RE1.Parent = game:GetService("ReplicatedStorage")
RE1.Name = "4590863"
local function REfired(plr)
local sound = game:GetService("ServerStorage"):WaitForChild("Sound")
local soundClone = sound:Clone()
soundClone.Parent = game:GetService("ServerStorage")
local plrName = plr.Name
sound.Parent = workspace[plrName].Head
end
RE1.OnServerEvent:Connect(REfired)
Try to be more descriptive with variable names too, can be difficult to read your code and help you with non-meaningful ones.
The reason @LegendOJ1 's code works is because when you fire a remote event, the roblox already makes the first parameter be the player who fired the remote event.
--local script:
event:FireServer(VariableA, VariableB)
--Server script:
event.OnServerEvent:Connect(function(plr, VariableA, VariableB) --roblox on default makes the plr the first parameter
end)
local VariableA -- We just define our variables outside the function, that way we can use it all over the script
local VariableB -- We just define our variables outside the function, that way we can use it all over the script
function TestFunction(SentA, SentB) -- just imagine this being your remote function instead.
VariableA = SentA -- We update the variable to be equal to what's sent
VariableB = SentB -- We update the variable to be equal to what's sent
end)
-- anything below this line, is only to demonstrate it in action.
spawn(function()
while task.wait(1)
print(VariableA,VariableB) -- just so you can see that the VariableA and VariableB have changed
end
end)
task.wait(5) -- waits 5 seconds before we update the Variables
TestFunction("Apple", "Banana") -- Take this as the signal that gets fired, just for test purposes
Well playing the sound on the server when the player dies doesn’t need a remote event, if I don’t get it wrong.
local Players = game:GetService("Players")
local function CharDied()
warn("Char died")
end
Players.PlayerAdded:Connect(function(plr)
plr.CharacterAdded:Connect(function(Character)
Character:WaitForChild("Humanoid").Died:Connect(CharDied)
end)
plr:LoadCharacter()
end)
Edit: I edited it a bit more after seeing @TortenSkjold’s code. I think a combination of both is more efficient.
I have changed a bit in Hex’s code, since some of it was wrong. I have also noted down everything.
local Players = game:GetService("Players") -- Gets the service Players
Players.PlayerAdded:Connect(function(plr) -- Function that is called every time a new player joins the game
plr.CharacterAdded:Connect(function(Character) -- Function that is called every time the new player's character is spawned in.
Character:WaitForChild("Humanoid").Died:Connect(function() -- Function that is called everytime the new player's character dies.
warn("Char died") -- Code that runs when the function is called
task.wait(3) -- waits 3 seconds before loading the new player's character again
Player:LoadCharacter -- Loads the new player's character. Make sure to turn off auto character loading in the "Players" tab in the Explorer.
end)
end)
Player:LoadCharacter -- Loads the new player's character. Make sure to turn off auto character loading in the "Players" tab in the Explorer.
end)
Edit: You will insert the play sound code at the “Char died” line.
I personally always load character manually. The reason for this is that I always have code, that I want initialized, before I load the character of the joining player.
Having the CharDied function outside as a seperate function is not needed for two reasons. We’re only calling the function once, and the code within the function is not really that long, meaning that what it does is easily readable.
You can always change the loading time yourself, to be honest. And the outside function is something I got used to doing lol Most of the time it is necessary to easily edit and read the code if the OP wants to edit it or add more death effects it’ll be a burden for them to read as well. Even with the current state of the script it’s messy.
local Players = game:GetService("Players")
local function CharDied()
local s = game:GetService("ServerStorage"):WaitForChild("Sound")
local xy = s:Clone()
xy.Parent = game:GetService("ServerStorage")
local xplr = tostring(plr)
s.Parent = workspace[xplr].Head
end
Players.PlayerAdded:Connect(function(plr)
plr.CharacterAdded:Connect(function(Character)
Character:WaitForChild("Humanoid").Died:Connect(CharDied)
end)
plr:LoadCharacter()
end)
local Players = game:GetService("Players")
local function CharDied()
warn("Char died")
end
Players.PlayerAdded:Connect(function(plr)
plr.CharacterAdded:Connect(function(Character)
Character:WaitForChild("Humanoid").Died:Connect(CharDied)
local sound = game:GetService("ServerStorage"):WaitForChild("Sound")
local soundClone = sound:Clone()
soundClone.Parent = game:GetService("ServerStorage")
local plrName = plr.Name
sound.Parent = workspace[plrName].Head
end)
plr:LoadCharacter()
end)