#Local Scripts and Servers
Local scripts only affect the client, or your computer. It doesn't make any changes to the server itself, therefore no one else sees the change and the server says that your health never changes. Use a regular script instead.
#Communication between Clients and Server
Use a RemoteEvent. That way, a local script and server script can communicate and share data with each other. Here's an example of using a RemoteEvent. The remote event is inside of the server script (server script = regular script) inside of ServerScriptService.
script.RemoteEvent.OnServerEvent:Connect(function(player)
print(player.Name.." fired the server event!")
end)
Local Script:
game:GetService("ServerScriptService").Script.RemoteEvent:FireServer()
Output:
Player1 fired the server event!
#UserInputService and ContextActionService
The KeyDown method should not be used. Opt for UserInputService or ContextActionService instead! The reason we use these instead of KeyDown is because KeyDown can only check for keyboard presses. UserInputService can detect touchscreen and Xbox controller inputs. ContextActionService can detect multiple keys being pressed at once and other key combinations. Since we're only using one key, we're going to use UserInputService. These services must be used in a Local Script, as the server is not allowed to detect what keys you are pressing.
local UIS = game:GetService("UserInputService")
UIS.InputBegan:Connect(function(key)
if key.UserInputType == Enum.UserInputType.Keyboard then
print("You're pressing the "..key.KeyCode.." key.")
end
end)
#Combining UIS and RemoteEvents
Server Script/Regular Script:
script.RemoteEvent.OnServerEvent:Connect(function(player)
print(player.Name.." clicked the C button.")
end)
Local Script:
local UIS = game:GetService("UserInputService")
local cooldown = false
UIS.InputBegan:Connect(function(key)
if key.UserInputType == Enum.UserInputType.Keyboard and not cooldown then
if key.KeyCode == Enum.KeyCode.C then
cooldown = true
game:GetService("ServerScriptService").Script.RemoteEvent:FireServer()
wait(2)
cooldown = false
end
end
end)
#Final Product
Server Script/Regular Script:
script.RemoteEvent.OnServerEvent:Connect(function(player)
local humanoid = player.Character:FindFirstChild("Humanoid")
if humanoid then
local sparks = Instance.new("Sparkles")
game:GetService("Debris"):AddItem(sparks, 2)
sparks.SparkleColor = Color3.new(1,1)
sparks.Parent = player.Character:FindFirstChild("HumanoidRootPart")
humanoid.Health = humanoid.Health+10
end
end)
Local Script:
local UIS = game:GetService("UserInputService")
local cooldown = false
UIS.InputBegan:Connect(function(key)
if key.UserInputType == Enum.UserInputType.Keyboard and not cooldown then
if key.KeyCode == Enum.KeyCode.C then
cooldown = true
game:GetService("ServerScriptService").Script.RemoteEvent:FireServer()
wait(2)
cooldown = false
end
end
end)
Few things to note:
I use Debris instead of Destroy to remove the sparkles. AddItem essentially does the same thing as Destroy, except after a certain amount of time. I put 2 seconds in the second argument, the sparks will be destroyed in 2 seconds. I said Color3.new(1,1) instead of 255,255, 0. If you're using Color3.new, every number will be multiplied up by 255, so it's actually equal. I left the last argument blank, because if you leave it blank then it will default to 0. Use Color3.fromRGB instead of Color3.new if you are using numbers 0-255 instead of 0-1.
Hope it helps!
1 comment
just a note: mouse.KeyDown is deprecated and you should use UserInputService's InputBegan event