Hello guys,
I have come across a weird bug and I hope you can guide me to solve this issue.
Brief description
When a player touches a part specific name, it will fire off a remote event from the client to the server and the remote event will add one point to the player leaderstats.
Remote event script (server script)
local amountGive = 1
game.ReplicatedStorage["Level 1 Orb"].OnServerEvent:Connect(function(player)
local character = player.Character or player.CharacterAdded:Wait()
if character then
local humanoid = character:FindFirstChild("Humanoid")
if humanoid then
player.leaderstats.Jump.Value += amountGive
print(amountGive)
humanoid.JumpHeight = player.leaderstats.Jump.Value
end
end
end)
Firing remote event script (local script)
local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")
humanoid.Touched:Connect(function(hit)
if hit.Name == "Lvl 1 JumpOrb" then
game.ReplicatedStorage["Level 1 Orb"]:FireServer()
hit:Destroy()
end
end)
Picture of the issue
Brief description of the issue
When I fire a remote event from the client side when I touch a part specific name, it will add more then one point to the user who fire it from a touch event. I tried to destroy the touched part before and after the remote event was fired but it is giving the same result.
What I want to achieve
When I fire a remote event from the client side when I touch a part specific name, it should give me back one point.
Thank you for reading my post and hope you can guide me on this.
Hi D4rk,
Thank you for the response. I would like to add a point to the player. The issue is the remote event script is giving out more than one point to the plater who fired the remote event.
local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")
local Stepped = false
humanoid.Touched:Connect(function(hit)
if hit.Name == "Lvl 1 JumpOrb" and Stepped == true then
Stepped = true
game.ReplicatedStorage["Level 1 Orb"]:FireServer()
hit:Destroy()
end
end)
You need to add a debounce to your Touched event like so:
local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")
local debounce
humanoid.Touched:Connect(function(hit)
if debounce then return end
if hit.Name == "Lvl 1 JumpOrb" then
debounce = true
game.ReplicatedStorage["Level 1 Orb"]:FireServer()
hit:Destroy()
task.wait(1)
debounce = nil
end
end)
Edit: @90poitu Although if you’d like to disable the Touched event from firing if the right condition is met and reward the point once you’ll need to do something like this:
local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")
local connection
connection = humanoid.Touched:Connect(function(hit)
if hit.Name == "Lvl 1 JumpOrb" then
connection:Disconnect()
connection = nil
game.ReplicatedStorage["Level 1 Orb"]:FireServer()
hit:Destroy()
end
end)
I don’t think this method will work for the system I want to implement. Basically this script will remove the connection / listener after hitting one specific part name.
I also noticed another problem with my local script. If two players are in the game, each player can see the orb and collect it without noticing another player has picked this up before that player pick up. I should add in another parameter when I am setting up my remote events.
Hello JohhnyLegoKing,
I have motified my scripts. Now when I fire off an event from the client side, it should delete the part that the humanoid touch
Server script (Remote event)
local replicatedStorageService = game.ReplicatedStorage
local level_1_remote_event = replicatedStorageService["Level 1 Orb"]
level_1_remote_event.OnServerEvent:Connect(function(player, givenAmount, partDestroy)
local character = player.Character or player.CharacterAdded:Wait()
if character then
local humanoid = character:FindFirstChild("Humanoid")
local jumpStat = player.leaderstats.Jump
if humanoid then
jumpStat.Value += givenAmount
print(givenAmount)
humanoid.JumpHeight = jumpStat.Value
partDestroy:Destroy()
end
end
end)
Local script
local replicatedStorageService = game:GetService("ReplicatedStorage")
local level_1_orb_remote_event = replicatedStorageService["Level 1 Orb"]
local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")
humanoid.Touched:Connect(function(hit)
if hit.Name == "Lvl 1 JumpOrb" then
level_1_orb_remote_event:FireServer(1, hit)
end
end)
But the problem still appearing when I touch a specific part name. When I touch a specific part name, it still give me more than one point to the player.
I have added a debounce on to my local script and fixed the part not destroying on the server but on the local machine.
Check if the player is a humanoid and is a character which I find it impossible to be
local replicatedStorageService = game.ReplicatedStorage
local level_1_remote_event = replicatedStorageService["Level 1 Orb"]
local function GivingOrb(player)
local character = player.Character or player.CharacterAdded:Wait()
if character then
local humanoid = character:FindFirstChild("Humanoid")
if humanoid then
return player.leaderstats.Jump
else
print(" Is not a humanoid")
end
else
print("Is not a character")
end
end
Now the part that was touch will now destroy on the server not locally
level_1_remote_event.OnServerEvent:Connect(function(player, part)
local jumpStatValue = GivingOrb(player)
if jumpStatValue then
jumpStatValue.Value += 1
part:Destroy()
print(jumpStatValue.Value)
end
end)
I have added a debounce to the local script and after task.wait(1) it should set the debounce back to false
local replicatedStorageService = game:GetService("ReplicatedStorage")
local level_1_orb_remote_event = replicatedStorageService["Level 1 Orb"]
local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")
local debounce = false
humanoid.Touched:Connect(function(hit)
if hit.Name == "Lvl 1 JumpOrb" and debounce == false then
level_1_orb_remote_event:FireServer(hit)
debounce = true
print(debounce)
task.wait(1)
debounce = false
print(debounce)
end
end)
Ah I see so the script was a LocalScript, in that case it’s correct that in order to destroy the orb for all players you’ll need to fire a remote event, or alternatively the Touched event does fire on server Scripts as well so from what I can tell you might be able to replace the LocalScript with a server Script without problems
Edit: @90poitu I think this server Script inside of StarterCharacterScripts should be able to do what you’d like without the need to use RemoteEvents:
local Players = game:GetService("Players")
local character = script.Parent
local humanoid = character.Humanoid
local player = Players:GetPlayerFromCharacter(character)
local jumpStat = player.leaderstats.Jump
local debounce
humanoid.Touched:Connect(function(hit)
if debounce then return end
if hit.Name == "Lvl 1 JumpOrb" then
debounce = true
--game.ReplicatedStorage["Level 1 Orb"]:FireServer()
jumpStat.Value += 1
hit:Destroy()
task.wait(1)
debounce = nil
end
end)