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)
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?
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)
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)
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
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)
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