Why am I asking for help?
I’ve made some simple lines of code in a local script, expecting it to act locally for 1 player, but that doesn’t seem to be the case.
How does it work?
it’s basically an oxygen bar for a space game. The script functions when a player touches a part in the workspace, when this happens the oxygen bar starts to go down, and when it reaches a certain goal (0%) the player starts to take damage over time.
What is the Issue?
the problem is, the local script functions for every player in the game even though it’s local, I searched for a solution but couldn’t find any.
local Frame = script.Parent.OxFrame
local Bar = Frame.OxBar
local Lable = Frame.OxLabel
local Player = game.Players.LocalPlayer
local Stoping = false
game.Workspace.O2Lose.Touched:Connect(function()
if Stoping then
return
end
Stoping = true
for i = 442 , -55 , -5 do
Bar.Size = UDim2.new(0, i,0, 35)
Lable.Text -= 1
wait(0.01)
end
if Lable.Text == "0" then
while wait(0.5) do
Player.Character.Humanoid:TakeDamage(10)
end
end
end)
I hope to find a solution.
Thank You.- Why am I asking for help?
I’ve made some simple lines of code in a local script, expecting it to act locally for 1 player, but that doesn’t seem to be the case.
How does it work?
it’s basically an oxygen bar for a space game. The script functions when a player touches a part in the workspace, when this happens the oxygen bar starts to go down, and when it reaches a certain goal (0%) the player starts to take damage over time.
What is the Issue?
the problem is, the local script functions for every player in the game even though it’s local, I searched for a solution but couldn’t find any.
local Frame = script.Parent.OxFrame
local Bar = Frame.OxBar
local Lable = Frame.OxLabel
local Player = game.Players.LocalPlayer
local Stoping = false
game.Workspace.O2Lose.Touched:Connect(function()
if Stoping then
return
end
Stoping = true
for i = 442 , -55 , -5 do
Bar.Size = UDim2.new(0, i,0, 35)
Lable.Text -= 1
wait(0.01)
end
if Lable.Text == "0" then
while wait(0.5) do
Player.Character.Humanoid:TakeDamage(10)
end
end
end)
If you want the script to work with only one player. You could define the player object and do an if statement to check if their userid or username is the player you want it to work for.
Example:
if player.Name == “Player” then
OR
if player.UserId == 1234 then
This means that the script will function when the player is named “Matt” for example.
but in my case, i don’t know who’s the player that is going to touch the part.
here’s a short explanation:
when 1 player touches the part that is supposed to make the script function, all the players start losing Oxygen and health in the same time.
I need the script to function or affect only the player that touched the part, and not all the players in the game.
Have a script (not localscript) that detects when a player touches the part. When they touch it, fire a RemoteEvent to the client, using :FireClient(player). Make sure to get the player’s name from touched event, and use it in the :FireClient() parameters as shown above, so that it knows what player to send it to.
Then have a localscript that receives the RemoteEvent with OnClientEvent:Connect, and then use your code for your oxygen feature. The idea would look sorta like this:
Script:
part.Touched:Connect(function(player)
remoteEvent:FireClient(player)
end
LocalScript:
remoteEvent.OnClientEvent:Connect(function(player)
-- Your oxygen stuff here
end
I believe there’s more to it with that - specifically what parameter to pass through the RemoteEvent for “player”. Anyways, I hope I was of some help!
Thx for your reply,
I’ve never used Events in Roblox studio before, I know what it is and how it works, but I’m not sure how to use it, I started watching some tutorials about it since I thought of the same suggestion as yours, but I wasn’t sure if it’s the right thing to use for my script, but now that you cleared that out, ill try using some of that “RemoteEvent” thing on my script, and see if it works.
There is a better way of doing it, give me one sec.
local Frame = script.Parent.OxFrame
local Bar = Frame.OxBar
local Lable = Frame.OxLabel
local Player
local Stoping = false
game.Workspace.O2Lose.Touched:Connect(function(hit)
Player = game.Players.LocalPlayer
if hit and hit.Parent:FindFirstChild("Humanoid") then
if Player.Name == hit.Parent.DisplayName then
if Stoping then
return
end
Stoping = true
for i = 442 , -55 , -5 do
Bar.Size = UDim2.new(0, i,0, 35)
Lable.Text -= 1
wait(0.01)
end
if Lable.Text == "0" then
while wait(0.5) do
Player.Character.Humanoid:TakeDamage(10)
end
end
end
end
end)
Not tested, but you really only need to check the player name with whoever touched the part.
In the local script, you could write the line:
if game.Players.LocalPlayer.UserId ~= 101 then return end
before everything in the script to stop it from running if the localplayer isn’t the right person.
Hey, that’s a smart way of doing it! It’s pretty cool seeing how people think differently and come up with different solutions. I’ll have to keep that way in mind as well for future times.