So I’m trying to fire an event when a part is touched but it won’t fire.
Local Script
local Level = game.ReplicatedStorage.Events:WaitForChild("Level")
script.Parent.Touched:Connect(function()
print("fired from client - client")
Level:FireServer()
end)
Server Script
game.ReplicatedStorage.Events.Level.OnServerEvent:Connect(function(Player)
print("recieved fire event - server")
Player.leaderstats.Level.Value = Player.leaderstats.Level.Value + 1
end)
Output
Nothing- Not even the print()
What I’ve tried:
Thanks for your time.
1 Like
The local script won’t run if it is a descendant of the workspace.
Where is the LocalScript located?
If you’re trying to fire this remote when the LocalScript is in the Workspace, it will not run. You also don’t need to fire a remote event to increase the leaderstats when a part is touched. You can handle this all in a normal script without the use of a RemoteEvent, as seen in the code example below. This also minimizes the risk of exploiters firing a RemoteEvent that will give them more points. In a lot of cases, you do actually have to use a RemoteEvent for Client - Server
or Server - Client
communication but in this case it’s not necessary, and can all be handled in a normal server script.
---> Script inside the part that is going to be touched
script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChildOfClass("Humanoid") then
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
player.leaderstats.Level.Value += 1
end
end)
1 Like
The Local Script is located inside a Part and the Part is located in Workspace.
Move it to StarterPlayerScripts.
Edit: Look at @LegendOJ1’s solution.
Yeah so then that won’t run because it’s a descendant of the workspace. You could put a normal script as the child of the part instead:
local db = false
local cooldown = 0.5
script.Parent.Touched:Connect(function(hit)
local humanoid = hit.Parent:FindFirstChild("Humanoid")
if humanoid and db == false then --check if it is a character
db = true
local player = game.Players:GetPlayerFromCharacter(hit.Parent) --get the player from the character
player.leaderstats.Level.Value += 1
wait(cooldown)
db = false
end
end)
1 Like
Thanks, I thought using a Remote Event would be good to use in this case - my mistake.
1 Like