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

Script works in solo mode but not on servers?

Finished making a 'drowning' script that runs when your head is inside this brick, and makes a 'waterblur' Gui visible that basically puts a blue tint on your screen like you're underwater and another Gui pops up that is your air meter, and when it runs out you slowly start taking damage. When I run the script in Play Solo it works perfectly, but when I start a server and test it out, it doesn't work, and there isn't any errors in the output. Here is the script:

script.Parent.Touched:connect(function(hit)
	local air=1
	if hit.Name == "Head" then
		game.Players.LocalPlayer.PlayerGui.Waterblur.Frame.Visible = true
		game.Players.LocalPlayer.PlayerGui.Drown.Frame.Visible = true
		while wait(.1) do
			air = air - 0.01
			game.Players.LocalPlayer.PlayerGui.Drown.Frame.TextLabel.Size = UDim2.new(air, 0, 1, 0)
			if game.Players.LocalPlayer.PlayerGui.Drown.Frame.Visible == false then
				game.Players.LocalPlayer.PlayerGui.Drown.Frame.TextLabel.Size = UDim2.new(1, 0, 1, 0)
				break
			end
			if air <= 0 then
				repeat
					hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - 10
					wait(1)
				until hit.Parent.Humanoid.Health == 0 or game.Players.LocalPlayer.PlayerGui.Drown.Frame.Visible == false
				break
			end
		end
	end
end)



script.Parent.TouchEnded:connect(function(hit)
	if hit.Name == "Head" then
		game.Players.LocalPlayer.PlayerGui.Waterblur.Frame.Visible = false
		game.Players.LocalPlayer.PlayerGui.Drown.Frame.Visible = false
	end
end)

2 answers

✓ Accepted answer

You're doing something really wrong here. The reason this worked in solo is because everything is ran locally there, meaning you're not testing the server.

  1. For the Touched event, this is ran in the server, meaning you cannot assign it to a local script rather use a server script.

  2. You cannot use LocalPlayer in a serverscript.

Solution:

script.Parent.Touched:connect(function(hit)
    local air=1
    if hit.Name == "Head" then
		local plr - game.Players:GetPlayerFromCharacter(hit.Parent)
		if plr then
			plr.PlayerGui.Waterblur.Frame.Visible = true
			plr.PlayerGui.Drown.Frame.Visible = true
			while wait(.1) do
				air = air - 0.01
				plr.PlayerGui.Drown.Frame.TextLabel.Size = UDim2.new(air, 0, 1, 0)
				if plr.PlayerGui.Drown.Frame.Visible == false then
					plr.PlayerGui.Drown.Frame.TextLabel.Size = UDim2.new(1, 0, 1, 0)
					break
				end
				if air <= 0 then
					repeat
						hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - 10
						wait(1)
					until hit.Parent.Humanoid.Health == 0 or plr.PlayerGui.Drown.Frame.Visible == false
					break
				end
			end
		end
    end
end)



script.Parent.TouchEnded:connect(function(hit)
    if hit.Name == "Head" then
		local plr - game.Players:GetPlayerFromCharacter(hit.Parent)
		if plr then
			plr.PlayerGui.Waterblur.Frame.Visible = false
			plr.PlayerGui.Drown.Frame.Visible = false
		end
    end
end)

1 comment

0

Alright, it worked. I see my mistake, sorry about that still a little new to the whole local/server thing :P. So basically in a local script you would want to get the player using LocalPlayer and in a server script use GetPlayerFromCharacter?

The problem is that you're using "LocalPlayer" on a script whose ancestor is the Workspace. That means that this script is treated like a server script. Even if you've made this a LocalScript, it won't run.

You have two options:

  1. You can simply make this a LocalScript, put it inside game.StarterGui, and change the first line to be "game.Workspace.Water.Touched" (or whatever the path to your part is).

  2. Split the script into a server-side script and a client-side script. The server would be responsible for telling the client when the client enters and leaves water, and also for telling the client how much air is left. The client would just be responsible for updating the gui. You would need to use RemoteEvents to transmit this information.

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