ServerChatmessage not sending

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)
1 Like

Oh thank you so much! where do you think I would be able to put the local script tho?

StarterPlayerScripts or StarterCharacterScripts will work.

1 Like

Make sure to copy the edits, just edited now.

1 Like

Alright thank you so much ill go try it out

When I try it I get the error “Char is not a valid member of JackFr_ost”

I put “Char” instead of “Character” for some reason, try now.

1 Like

Thanks that one works now but im getting this error for the server message
“OnServerEvent can only be used on the server - Client - LocalScript:7”

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.

1 Like

Same its midnight here, Thank you for your help tho, brb

Perfect, it works great, thank you so much!!

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.

1 Like

No problem.

You can mark your own post as solved here, since it’s similar to this thread.

1 Like

Thank you for your suggestion ill be sure to use that in future projects! :slight_smile:

1 Like

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)
1 Like

Sorry I didnt see that you replied, Ill go try it out tysm!

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)

This one works really good :))

Alright, if that one works stick with it and no problem about not seeing the reply.

1 Like