Hi, I currently have a remote event that clones a random model from ServerStorage and positions it above a part in the workspace labeled ‘CarSpawn’. My intent is to check if the players name matches the player who cloned the model then delete this model when the player collides with the part ‘destroyCar’. It returns “StringValue,” not the players name, and doesn’t delete the model.
There are no errors.
-- Server Script
local items = game.ServerStorage:GetChildren()
local randomItem = items[math.random(1, #items)]
local destroyCar = game.Workspace.destroyCar
local remoteEvent = game.ReplicatedStorage.SpawnCar
local playerValue = randomItem.StringValue
local function spawnCar(player)
carClone = randomItem:Clone()
carClone.Parent = workspace
carClone.PrimaryPart.CFrame = game.Workspace.CarSpawn.CFrame + Vector3.new(0,5,0)
playerValue.Value = player.Name
-- print(playerValue)
end
local function deleteCar(player)
if playerValue.Value == player.Name then
carClone:Destroy()
end
end
destroyCar.Touched:Connect(deleteCar)
remoteEvent.OnServerEvent:Connect(spawnCar)
.Touched first parameter is the Instance which touched the part, to get the player use :GetPlayerFromCharacter():
local Players = game:GetService("Players")
local DestroyCar = workspace.DestroyCar
DestroyCar.Touched:Connect(function(hit)
if hit and hit.Parent:FindFirstChildOfClass("Humanoid") then
local Client = Players:GetPlayerFromCharacter(hit.Parent)
end
end)
I was able to discern the issue, here’s the solution (it was the concoction of your answers, thank you all):
-- Server Script
local items = game.ServerStorage:GetChildren()
local randomItem = items[math.random(1, #items)]
local destroyCar = game.Workspace.destroyCar
local remoteEvent = game.ReplicatedStorage.SpawnCar
local playerValue = randomItem.StringValue
local Players = game:GetService("Players")
local DestroyCar = workspace.destroyCar
local function spawnCar(player)
carClone = randomItem:Clone()
carClone.Parent = workspace
carClone.PrimaryPart.CFrame = game.Workspace.CarSpawn.CFrame + Vector3.new(0,5,0)
playerValue.Value = player.Name
-- print(playerValue.Value)
end
DestroyCar.Touched:Connect(function(hit)
if hit and hit.Parent:FindFirstChildOfClass("Humanoid") then
local Client = Players:GetPlayerFromCharacter(hit.Parent)
if playerValue.Value == Client.Name then
carClone:Destroy()
end
end
end)
remoteEvent.OnServerEvent:Connect(spawnCar)