Hey there,
Ive got this script here that gives a player +1 leaderstat when they touch a part, and I’m trying to make a server chat message announce that somebody won. Im using a remote event for the chat message and it isnt working.
Script 1:
local event = game.ReplicatedStorage.Events.Won
event.OnServerEvent:Connect(function(plr)
local message = game.StarterGui:SetCore( "ChatMakeSystemMessage", { Text = "[Server]: "..plr.Name.. " just won!", Color = Color3.fromRGB(255, 6, 51), Font = Enum.Font.Arial, FontSize = Enum.FontSize.Size24 } )
print("Message sent")
end)
Script 2:
amnt = 1
message = game.ReplicatedStorage.Events.Won
local debounce = false
function onTouched(part)
local h = part.Parent:findFirstChild("Humanoid")
local plr = part.Parent
if (h~=nil) then
local thisplr = game.Players:findFirstChild(h.Parent.Name)
if (thisplr~=nil) then
local stats = thisplr:findFirstChild("leaderstats")
if (stats~=nil) then
local score = stats:findFirstChild("Wins")
if (score~=nil) then
if not debounce then
debounce = true
message:FireAllClients()
score.Value = score.Value + amnt
wait(60)
debounce = false
end
end
end
end
end
end
script.Parent.Touched:connect(onTouched)
local players = game:GetService("Players")
local repStorage = game:GetService("ReplicatedStorage")
local wonEvent = repStorage:WaitForChild("Events"):WaitForChild("Won")
local debounce = false
function onTouched(part)
if part.Parent:FindFirstChild("Humanoid") then
local plr = players:GetPlayerFromCharacter(part.Parent)
local char = plr.Character
local hum = char:WaitForChild("Humanoid")
if plr then
local leaderstats = plr:WaitForChild("leaderstats")
if leaderstats then
local score = leaderstats:WaitForChild("Wins")
if score then
if debounce then
return
end
debounce = true
wonEvent:FireAllClients(plr)
score.Value += 1
task.wait(60)
debounce = false
end
end
end
end
end
script.Parent.Touched:connect(onTouched)
local players = game:GetService("Players")
local player = players.LocalPlayer or players.PlayerAdded:Wait()
local starterGui = game:GetService("StarterGui")
local repStorage = game:GetService("ReplicatedStorage")
local wonEvent = repStorage:WaitForChild("Events"):WaitForChild("Won")
wonEvent.OnClientEvent:Connect(function(plr)
local name = plr.Name
local message = starterGui:SetCore("ChatMakeSystemMessage", {
Text = "[Server]: "..name.." just won!",
Color = Color3.fromRGB(255, 6, 51),
Font = Enum.Font.Arial,
FontSize = Enum.FontSize.Size24
})
end)
Should be “OnClientEvent” I’ve replaced that in the edit, it’s late my end so excuse these mistakes (I usually don’t make them).
Should be working now.
You can use a BindableEvent instead of a RemoteEvent. In case you don’t know a BindableEvent is like a RemoteEvent but you can use it between the same type of script for example:
Sending a message from a Script to another Script
Sending a message from a LocalScript to another LocalScript
However, a RemoteEvent can only be used between Different types of scripts for example:
Sending a message from a Script to another LocalScript
Sending a message from a LocalScript to another Script
What I am trying to say is you can use a Script and a BindableEvent if you want instead to make it easier.
Oh yeah I found an issue with it, The debounce doesnt just work for 1 player but for the whole server so when 1 player touches it everyone needs to wait 60 seconds. Any idea on how to fix this?
Oh, I wasn’t sure if that would be an issue or not, here:
local players = game:GetService("Players")
local repStorage = game:GetService("ReplicatedStorage")
local wonEvent = repStorage:WaitForChild("Events"):WaitForChild("Won")
local playerDebounce = {}
function onTouched(part)
if part.Parent:FindFirstChild("Humanoid") then
local plr = players:GetPlayerFromCharacter(part.Parent)
local char = plr.Character
local hum = char:WaitForChild("Humanoid")
if plr then
local leaderstats = plr:WaitForChild("leaderstats")
if leaderstats then
local score = leaderstats:WaitForChild("Wins")
if score then
if table.find(playerDebounce, plr) then
return
end
table.insert(playerDebounce, plr)
wonEvent:FireAllClients(plr)
score.Value += 1
task.wait(60)
table.remove(playerDebounce, plr)
end
end
end
end
end
script.Parent.Touched:connect(onTouched)
local players = game:GetService("Players")
local repStorage = game:GetService("ReplicatedStorage")
local wonEvent = repStorage:WaitForChild("Events"):WaitForChild("Won")
local playerDebounce = {}
function onTouched(part)
if part.Parent:FindFirstChild("Humanoid") then
local plr = players:GetPlayerFromCharacter(part.Parent)
local char = plr.Character
local hum = char:WaitForChild("Humanoid")
if plr then
local leaderstats = plr:WaitForChild("leaderstats")
if leaderstats then
local score = leaderstats:WaitForChild("Wins")
if score then
if table.find(playerDebounce, plr) then
return
end
table.insert(playerDebounce, plr)
wonEvent:FireAllClients(plr)
score.Value += 1
task.wait(60)
for i, player in pairs(playerDebounce) do
if player == plr then
table.remove(playerDebounce, i)
end
end
end
end
end
end
end
script.Parent.Touched:connect(onTouched)