Help with CharacterAdded / RemoteEvent

Hello!
Can someone please help me, I’m having a problem with the CharacterAdded and RemoteEvents, I’ve been looking for a solution for this problem but I can’t find one that fits my problem.

So, the script was working correctly, but when I added the CharacterAdded to RemoteEvent the script is no longer working. does anyone know why this is happening? Thanks!

Scripts:

Server Script: (ServerScriptService)
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		ReplicatedStorage.Events.OwnedTool:FireClient(Player, Character)
	end)
end)
Local Script: (StarterGui)
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local Player = Players.LocalPlayer repeat wait() until Player
Local Character = Player.Character

ReplicatedStorage.Events.OwnedTool.OnClientEvent:Connect(function(Player, Character)
	local ToolName = script.Parent.Item.ToolName.Value
	local ToolPrice = script.Parent.Item.ToolPrice.Value
	local ToolButton = script.Parent.Item

	if ReplicatedStorage.PlayerToolFolder[Player.Name]:FindFirstChild(ToolName) then
		ToolButton.Price.Text = "OWNED"
	end
end)
1 Like

in line 6 in the local script you typed “Local” instead of “local”

1 Like

not sure why you did this but why did you make a variable for the Player & The Character in the local script since you assigned them in the remote event args?

1 Like

If @ayoub50 's solution didn’t work. did you have any errors?

1 Like

Hi, I’m sorry I did wrong with that Local, but I think the problem is only on these lines of the script:

Server Script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character) --Line that is not working
		ReplicatedStorage.Events.OwnedTool:FireClient(Player, Character) --Line that is not working
	end)
end)
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local Player = Players.LocalPlayer repeat wait() until Player
Local Character = Player.Character

ReplicatedStorage.Events.OwnedTool.OnClientEvent:Connect(function(Player, Character) --Line that is not working
	local ToolName = script.Parent.Item.ToolName.Value
	local ToolPrice = script.Parent.Item.ToolPrice.Value
	local ToolButton = script.Parent.Item

	if ReplicatedStorage.PlayerToolFolder[Player.Name]:FindFirstChild(ToolName) then
		ToolButton.Price.Text = "OWNED"
	end
end)
1 Like

yeah remove

local Player = Players.LocalPlayer repeat wait() until Player
local Character = Player.Character

as i said, you already assigned the args in the remote event’s args so no need to make variables for it

1 Like

also why did you put the player’s character in the args since you didn’t need it

1 Like

Ok wait im going to check that, thanks!

1 Like

I deleted the lines, but still not working yet maybe problem is in server script?

1 Like

This should work.

Server Script :

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function()
		ReplicatedStorage.Events.OwnedTool:FireClient(Player)
	end)
end)

Local Script :

local ReplicatedStorage = game:GetService("ReplicatedStorage")

ReplicatedStorage.Events.OwnedTool.OnClientEvent:Connect(function()
        local Player = game.Players.LocalPlayer
	local ToolName = script.Parent.Item.ToolName.Value
	local ToolPrice = script.Parent.Item.ToolPrice.Value
	local ToolButton = script.Parent.Item

	if ReplicatedStorage.PlayerToolFolder[Player.Name]:FindFirstChild(ToolName) then
		ToolButton.Price.Text = "OWNED"
	end
end)

Ok thanks, let me check it in Roblox Studio

1 Like

i had an error in the script, you can try again.

Yes that works, but when I respawn that says you need to buy the item again, I think I need to add in the server and local script the function(Player, Character) because when player respawns need to say that has the item and don’t need to buy it again

1 Like

make sure that the ScreenGui’s ResetOnSpawn Property Is set False.
also you must check if the player has the tool or not before firing the remote.

I tried to place it in StarterPlayerScripts but is not working, maybe I need to change something?

1 Like

I tried both in Roblox and Roblox Studio and is not working

1 Like

the issue is the characteradded function isn’t catching the first character when the player first joins the game so you need to setup a function for initial join and then for any characters that are reset or they change to

your server script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")

function SetupCharacter(Player,Character)
	ReplicatedStorage.Events.OwnedTool:FireClient(Player, Character) --Line that is not working
end
game.Players.PlayerAdded:Connect(function(Player)
	local Character = Player.Character or Player.CharacterAdded:Wait()  -- need to get character for intitial character setup
		SetupCharacter(Player,Character)  -- your intial or first call 
	Player.CharacterAdded:Connect(function(Character) --Line that is not working   --- this is not catching the first character when player first joins is why
		SetupCharacter(Player,Character)  -- if they reset or chance character it will setup the new character
	end)
end)

your client script from latest you posted

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Player = game.Players.LocalPlayer  -- i always do this at top of localscript

ReplicatedStorage.Events.OwnedTool.OnClientEvent:Connect(function()
	local ToolName = script.Parent.Item.ToolName.Value
	local ToolPrice = script.Parent.Item.ToolPrice.Value
	local ToolButton = script.Parent.Item

	if ReplicatedStorage.PlayerToolFolder[Player.Name]:FindFirstChild(ToolName) then
		ToolButton.Price.Text = "OWNED"
	end
end)
2 Likes

actually nvm, put it back in where it was

Ok give me a minute please ill see if that works

1 Like

I still having the problem that when the player respawns it says me that I need to buy the item again, I don’t know if its the local or server script issue, but it still, DataStore is working correctly just is problem of the local or server script

1 Like